poj 2406
来源:互联网 发布:写歌软件下载 编辑:程序博客网 时间:2024/05/17 02:58
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
abcdaaaaababab.
Sample Output
143
我的代码# include<stdio.h># include<string.h>
char ss[1000002];
int next[1000002],len;
void getNext()
{
int i=1,j=0;
next[1]=0;
while(i<len)
{
if(j==0||ss[i]==ss[j])
{
i++;j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
while(1)
{
int i,j,seg,sum=0;
scanf("%s",ss+1);
len=strlen(ss+1);
if(len==1&&ss[1]=='.')
break;
getNext();
seg=len-next[len];
for(i=seg;i<=len-seg;i+=seg)////拖沓。。。。
if(ss[i]!=ss[i+seg])
{
sum=1;
break;
}
if(len%seg==0&&sum==0)
printf("%d\n",len/seg);
else
printf("%d\n",1);
}
return 0;
}
、、大神的代码!!!!!
- #include<stdio.h>
- #include<string.h>
- #define max 1000000
- int next[max];
- char str1[max];
- int get_next(char *pat)
- {
- int j=0,k=-1;
- int len=strlen(pat);
- next[0]=-1;
- while(j<len)
- {
- if(k==-1||pat[j]==pat[k])
- next[++j]=++k;
- else
- k=next[k];
- }
- j=len-k;//如果最后一个位置不匹配,那么就会滚到len-k的位置,也就是最小重复字串的长度。
- if(len%j==0)
- return len/j;
- else
- return 1;
- }
- int main()
- {
- while(scanf("%s",&str1)!=EOF)
- {
- if(str1[0]=='.')
- break;
- printf("%d\n", get_next(str1));
- }
- return 0;
- }
- POJ 2406
- POJ 2406
- poj 2406
- POJ 2406
- poj 2406
- poj 2406
- poj 2406
- poj 2406
- poj 2406
- poj 2406
- 【POJ 2406 】
- POJ 2406
- poj 2406
- POJ 2406
- poj 2406
- POJ-2406
- POJ 2406
- poj--2406
- C/C++源代码到可执行程序的过程详解
- 推荐12个漂亮的CSS3按钮实现方案
- python实现自顶向下,自底向上
- (1)了解.net、Visual Studio的设置
- 【PAT】1041. Be Unique (20)
- poj 2406
- 汇编语言的学习(CPU对存储器的读写)
- C++ 静态存储区 栈 堆的区别
- uva 10763 Foreign Exchange
- asp.net错误.在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错
- (2)变量、数据类型、转义符、类型转换
- 高中生活-第6篇–批评和表扬之间的对决
- 你看得懂的海明码校验和纠错原理
- Java类与类之间的关系