标签:body strong ababc possible nbsp cep asi sea ring
http://poj.org/problem?id=2752
Seek the Name, Seek the Fame
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 27512 |
|
Accepted: 14244 |
Description
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:
Step1. Connect the father‘s name and the mother‘s name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father=‘ala‘, Mother=‘la‘, we have S = ‘ala‘+‘la‘ =
‘alala‘. Potential prefix-suffix strings of S are {‘a‘, ‘ala‘, ‘alala‘}.
Given the string S, could you help the little cat to write a program to
calculate the length of possible prefix-suffix strings of S? (He might
thank you by giving your baby a name:)
Input
The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
For
each test case, output a single line with integer numbers in increasing
order, denoting the possible length of the new baby‘s name.
Sample Input
ababcababababcabab
aaaaa
Sample Output
2 4 9 18
1 2 3 4 5
Source
题意:求字符串所有的相同前缀与后缀;
思路:我们知道next数组是求最长前缀与后缀,如果要求所有,我们就要向前递归next数组直到为0,即可找出所有长度的前缀与后缀
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[1000009];
int next[1000009];
int num[1000009];
void getnext(char *a , int len , int *next)
{
next[0] = -1 ;
int k = -1 , j = 0 ;
while(j < len)
{
if(k == -1 || a[k] == a[j])
{
k++;
j++;
// if(a[k] != a[j])
next[j] = k;//next数组不能优化
// else
// next[j] = next[k];*/
}
else
k = next[k];
}
}
int main()
{
while(~scanf("%s" , a))
{
memset(next , 0 , sizeof(next));
memset(num , 0 , sizeof(num));
int len = strlen(a);
getnext(a , len , next);
int l = 0 ;
num[l++] = len ;
while(next[len] > 0)//向前递归next数组
{
num[l++] = next[len];
len = next[len];
}
for(int i = l - 1 ; i > 0 ; i--)
{
printf("%d " , num[i]);
}
printf("%d\n" , num[0]);
}
return 0 ;
}
kmp(所有长度的前缀与后缀)
标签:body strong ababc possible nbsp cep asi sea ring
原文地址:https://www.cnblogs.com/nonames/p/11294338.html