POj 2406 Power Strings
2017-02-04 15:57
218 查看
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.
这道题是考察对next数组性质的应用。题意相当于是求一个长字符串中循环节的个数。
由于我们知道next数组中存的是一个位置(假设next[j]的值为k,对应的字符串为M,如果k>0,那么M[0....k-1]和M[j-k.....j-1]是相同的,并且0...k-1这个序列一定是最长的),比如a b c a b c d(next值:-1 0 0 0 1 2 3 ),由next[6]=3可知,M[0..2]=M[3..6],这就找到了循环节,于是我们思考从next数组作为切入点,来找到一种方法来求得循环节的个数。
看看next数组的一个性质:next始终是从-1开始增加(在变为0之前)。这会导致一个有趣的现象:指针回溯的位置,最远都是在一个完整的循环节之后。其实由定义也能发现,如果最远回溯到了字符串开头,就会导致j=k,与next数组的定义中的0<k<j矛盾。这样,就留出来了一个循环节的长度,如果总长度是这个循环节长度的整数倍,那么循环节的个数就是这个倍数。反之,说明这个字符串并不是在不停地循环,而是在某些位置加入了一个或几个不"和谐"的字符,导致指针无法回溯到第一个循环节之后,这样,输出1就可以了。
#include<stdio.h>
#include<string.h>
#define MAX_LEN 1000005
int get_next(void);
char dest[MAX_LEN];
int next[MAX_LEN];
int main()
{
while(scanf("%s",dest)!=EOF&&dest[0]!='.')
{
int len=get_next();
int flag=len%(len-next[len]);
if(flag==0)
{
printf("%d\n",len/(len-next[len]));
}
else
{
printf("1\n");
}
}
return 0;
}
int get_next(void)
{
int len=strlen(dest),i=0,j=-1;
next[0]=-1;
while(i<len)
{
if(j==-1||dest[i]==dest[j])
{
i++;j++;
next[i]=j;
}
else
{
j=next[j];
}
}
return len;
}
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.
这道题是考察对next数组性质的应用。题意相当于是求一个长字符串中循环节的个数。
由于我们知道next数组中存的是一个位置(假设next[j]的值为k,对应的字符串为M,如果k>0,那么M[0....k-1]和M[j-k.....j-1]是相同的,并且0...k-1这个序列一定是最长的),比如a b c a b c d(next值:-1 0 0 0 1 2 3 ),由next[6]=3可知,M[0..2]=M[3..6],这就找到了循环节,于是我们思考从next数组作为切入点,来找到一种方法来求得循环节的个数。
看看next数组的一个性质:next始终是从-1开始增加(在变为0之前)。这会导致一个有趣的现象:指针回溯的位置,最远都是在一个完整的循环节之后。其实由定义也能发现,如果最远回溯到了字符串开头,就会导致j=k,与next数组的定义中的0<k<j矛盾。这样,就留出来了一个循环节的长度,如果总长度是这个循环节长度的整数倍,那么循环节的个数就是这个倍数。反之,说明这个字符串并不是在不停地循环,而是在某些位置加入了一个或几个不"和谐"的字符,导致指针无法回溯到第一个循环节之后,这样,输出1就可以了。
#include<stdio.h>
#include<string.h>
#define MAX_LEN 1000005
int get_next(void);
char dest[MAX_LEN];
int next[MAX_LEN];
int main()
{
while(scanf("%s",dest)!=EOF&&dest[0]!='.')
{
int len=get_next();
int flag=len%(len-next[len]);
if(flag==0)
{
printf("%d\n",len/(len-next[len]));
}
else
{
printf("1\n");
}
}
return 0;
}
int get_next(void)
{
int len=strlen(dest),i=0,j=-1;
next[0]=-1;
while(i<len)
{
if(j==-1||dest[i]==dest[j])
{
i++;j++;
next[i]=j;
}
else
{
j=next[j];
}
}
return len;
}
相关文章推荐
- POJ - 2406 Power Strings —— next数组求循环节
- poj 2406 Power Strings(KMP求最大循环次数)
- poj 2406:Power Strings(KMP)
- poj 2406 Power Strings
- POJ 2406 Power Strings
- POJ-2406(Power Strings)(KMP())
- poj 2406 Power Strings
- POJ 2406 Power Strings
- POJ 2406 Power Strings
- poj 2406 Power Strings(KMP变形)
- poj 2406 Power Strings
- poj 2406:Power Strings
- POJ 2406 Power Strings
- poj 2406 Power Strings-字符串(kmp)
- POJ 2406 Power Strings
- poj 2406 Power Strings(kmp应用)
- poj 2406 Power Strings (用kmp代替后缀数组求解)
- poj------2406 Power Strings
- poj 2406 Power Strings
- poj 2406 Power Strings