Problem 1901 Period II
Accept: 215 Submit: 562
题目大意:就是问可以满足s[i]=s[p+i]的p的值
ac代码
Accept: 215 Submit: 562
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
For each prefix with length P of a given string S,if
S[i]=S[i+P] for i in [0..SIZE(S)-p-1],
then the prefix is a “period” of S. We want to all the periodic prefixs.
Input
Input contains multiple cases.
The first line contains an integer T representing the number of cases. Then following T cases.
Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.
Output
For each test case, first output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of periodic prefixs.Then output the lengths of the periodic prefixs in ascending order.
Sample Input
4oooacmacmacmacmacmafzufzufzufstostootssto
Sample Output
Case #1: 31 2 3Case #2: 63 6 9 12 15 16Case #3: 43 6 9 10Case #4: 29 12
Source
FOJ有奖月赛-2010年05月题目大意:就是问可以满足s[i]=s[p+i]的p的值
ac代码
RunID: 630249
UserID: kxh1995_joker
Submit time: 2015-08-24 10:52:32
Language: C++
Length: 690 Bytes.
Result: Accepted
#include<stdio.h>
#include<string.h>
char str[1000010];
int ans[1000010],next[1000010];
void getnext(char *s)
{
int i,j;
i=0;
j=-1;
next[0]=-1;
int len=strlen(s);
while(i<=len)
{
if(j==-1||s[i]==s[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
int t,c=0;
scanf("%d",&t);
while(t--)
{
int i;
scanf("%s",str);
int len=strlen(str);
getnext(str);
int cnt=0,temp=len;
while(next[temp]!=-1)
{
ans[cnt++]=next[temp];
temp=next[temp];
}
printf("Case #%d: %d\n",++c,cnt);
printf("%d",len-ans[0]);
for(i=1;i<cnt;i++)
printf(" %d",len-ans[i]);
printf("\n");
}
}