POJ 2751:Seek the Name, Seek the Fame(Hash)

                     Seek the Name, Seek the Fame

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24077   Accepted: 12558

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

POJ 2751:Seek the Name, Seek the Fame(Hash)_第1张图片

题意

给出一个字符串ch,求出ch中存在多少子串,使得这些子串既是ch的前缀,又是ch的后缀。从小到大依次输出这些子串的长度。 

思路

这个题好像很多都是用KMP写的,百度看了看代码,递归的那个过程不是太理解,就用Hash写了,感觉Hash的思路更简单

记得要提前预处理一下

AC代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
const int base=2333;
using namespace std;
char ch[maxn];
ull a[maxn];
ull b[maxn];
int vis[maxn];
int main(int argc, char const *argv[])
{
	a[0]=1;
	for(int i=1;i>(ch+1))
	{
		ms(b);
		int l=strlen(ch+1);
		b[1]=ch[1];
		for(int i=2;i<=l;i++)
			b[i]=b[i-1]+a[i-1]*ch[i];
		int k=0;
		for(int i=1;i<=l;i++)
		{
			if(b[i]*a[l-i]==(b[l]-b[l-i]))
				vis[k++]=i;
		}
		for(int i=0;i

 

你可能感兴趣的:(Hash,POJ)