首页 > 其他分享 >POJ2406-Power Strings

POJ2406-Power Strings

时间:2022-11-22 21:08:40浏览次数:42  
标签:string Power int len next 字符串 POJ2406 include Strings




Power Strings


Time Limit: 3000MS

 

Memory Limit: 65536K

Total Submissions: 55320

 

Accepted: 22983


Description


Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).


Input


Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.


Output


For each s you should print the largest n such that s = a^n for some string a.


Sample Input


abcd aaaa ababab .


Sample Output


1 4 3


Hint


This problem has huge input, use scanf instead of cin to avoid time limit exceed.


       题解:题意就是由一个字符串重复多遍组成,求最多重复次数。

       也就是求这个字符串的最小长度即可。很明显与KMP的fail数组有关,求出可以发现如果重复次数大于1,那么n-fail[n]必定是最小的字符串长度。随便列几次就可以发现了。

Code:

#include<cstdio>  
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1000005
int next[N],len;
int main()
{
char a[N];
while(~scanf("%s",a))
{
if(a[0]=='.')break;
len=strlen(a);
int i=0,j=-1;
next[0]=-1;
while(i<len)
{
if(j==-1||a[i]==a[j])
{
i++,j++;
next[i]=j;
}else j=next[j];
}
int k=len-next[len];
if(len%k==0)printf("%d\n",len/k);
else printf("1\n");
}
return 0;
}

标签:string,Power,int,len,next,字符串,POJ2406,include,Strings
From: https://blog.51cto.com/u_15888102/5878464

相关文章

  • Power BI 3 DAY
    目录M函数基本表达式注释基本表达式M函数基本变量类型自定义函数调用函数列表if表达式数据化结构列表结构(List){}记录结构(Record)[]表格结构(Table)M函数基本表达式注释单......
  • WordPress编辑器支持PowerPoint一键粘贴
    ​如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-......
  • 【Azure 应用服务】Azure Powershell Function 出错 The term 'Connect-AzAccount' is
    问题描述在AzureFunction中,执行Powershell的Function脚本时,先后出现1:[Error]ERROR:Theterm'Connect-AzAccount'isnotrecognizedasanameofacmdlet,functio......
  • WordPress编辑器支持PowerPoint一键上传
    ​ 当前功能基于PHP,其它语言流程大致相同 1.新增上传wordjson配置在ueditor\php\config.json中新增如下配置:     /* 上传word配置 */    "wordAction......
  • WordPress编辑器支持PowerPoint一键导入
    ​ 百度ueditor新增的将word内容导入到富文本编辑框的功能怎么没有啊,...ueditor实现word文档的导入和下载功能的方法:1、UEditor没有提供word的导入功能,只能说是粘贴复......
  • WordPress编辑器支持PowerPoint自动粘贴
    ​ 1.编辑器修改(可选)1.1在 ueditor/config.json 中添加代码块    /* 上传word配置 */    "wordActionName":"wordupload",/* 执行上传视频的action......
  • PowerDesigner入门使用操作
    PowerDesigner是一个做开发设计很常用的工具软件,同时还有Rose也可以,都是当前软件开发最著名的建模设计及软件之一,下面讲解简单的应用。步骤:1.现在各版本非常多,本教程使用16......
  • Power BI 1 DAY
    目录PowerBI(商业智能)分析BI分析步骤PowerQuery表数据结构区别主键PowerQuery中的纵向合并与横向合并销售一表和销售二表进行纵向合并为一张销售表。产品表通过产品编号......
  • C. Sum of Substrings题解
    C.SumofSubstrings题目大概意思,给你一个01串,求和最小,其中和是该串所有相邻字符所组成的十进制数的和。如:0110,sum=01+11+10=22。通过观察我们可以发现,除了第......
  • [参考]Powershell简单入门
    由于诸多样本都使用Powershell执行无文件攻击,加载本文可能会触发您的杀毒软件报告。本文不下载任何文件,不执行任何文件,请放心加载!!于实战中应用PowershellPowershell是一......