poj 2406 kmp算法巩固之next数组的再理解
2014-03-25 20:21
323 查看
http://poj.org/problem?id=2406
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
Sample Output
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
我学习的人家的,感觉还好,似乎很容易懂
#include <stdio.h>
#include <string.h>
#include <iostream>
const int N=1000005;
int next
,len;
char a
;
void get_next()
{
int i=0,j=-1;
next[0]=-1;
while(i<len)
{
if(j==-1||a[i]==a[j])
next[++i]=++j;
else
j=next[j];
}
}
int main()
{
while(~scanf("%s",a))
{
if(a[0]=='.')
break;
len=strlen(a);
get_next();
if(len%(len-next[len])!=0)
printf("1\n");
else
printf("%d\n",len/(len-next[len]));
}
return 0;
}
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.
我学习的人家的,感觉还好,似乎很容易懂
#include <stdio.h>
#include <string.h>
#include <iostream>
const int N=1000005;
int next
,len;
char a
;
void get_next()
{
int i=0,j=-1;
next[0]=-1;
while(i<len)
{
if(j==-1||a[i]==a[j])
next[++i]=++j;
else
j=next[j];
}
}
int main()
{
while(~scanf("%s",a))
{
if(a[0]=='.')
break;
len=strlen(a);
get_next();
if(len%(len-next[len])!=0)
printf("1\n");
else
printf("%d\n",len/(len-next[len]));
}
return 0;
}
相关文章推荐
- poj 2406:Power Strings(KMP算法,next[]数组的理解)
- POJ 2406 KMP算法next数组理解
- POJ - 2406_Power Strings _kmp算法next数组的应用
- POJ 3461 Oulipo 深刻理解KMP算法next数组含义
- poj 1961 2406 让你彻底理解KMP的next[]数组的两个题目
- KMP算法 一般详解,NEXT数组 一般理解 k = next[k] ——综合转载
- POJ 1961 Period KMP算法next数组的应用
- POJ - 2406 Power Strings(next数组)
- poj 2406 Power Strings[kmp,next数组的应用]
- POJ2406 Power Strings(KMP next数组的运用)
- KMP算法next数组通俗理解,适合考研及基础学习者
- 对kmp算法next数组的一些简单理解
- 字符串匹配——KMP算法中的next数组理解
- poj 2406 KMP算法中next的一个性质
- KMP算法next数组计算的理解——菜鸟福音
- poj 2406 Power Strings(kmp next数组的应用)
- POJ 2406-巧用next数组
- POJ 2752 KMP算法中next数组的应用
- POJ 2406 Power Strings KMP算法之next数组的应用
- 【next数组应用】POJ 2406 Power Strings