【哈希和哈希表】Seek the Name, Seek the Fame
题目描述
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:)
输入
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.
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
输出
For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.
样例输入
ababcababababcabab
aaaaa
样例输出
2 4 9 18
1 2 3 4 5
【题意】:
对于一个字符串s,找出所有相同的前缀后缀长度.
【题解】:
利用hash的方法,直接取两端的值。
代码是自己刚学hash时写的,所以有点乱,代码是参考wsy的。
#include<bits/stdc++.h>
#define Mp make_pair
#define F first
#define S second
using namespace std;
const int N = 1e6+;
const int M1 = 1e9+ , M2 = 1e9+;
typedef long long ll;
typedef struct Node{
long long first ,second; Node (){}
Node ( ll u,ll v){ first = u , second = v ;}
}PII;
//typedef pair<ll,ll> PII; const PII base{M2,M1},p{M1,M2},One{1ll,1ll},Zero{0ll,0ll}; PII operator - (PII u,PII v){
return Node( (u.first-v.first+p.first)%p.first ,(u.second-v.second+p.second)%p.second );
}
PII operator * ( PII u , PII v ){
return Node( (u.first*v.first)%p.first , (u.second*v.second)%p.second );
}
PII operator + ( PII u , PII v ){
return Node( (u.first+v.first)%p.first , (u.second+v.second)%p.second );
}
PII operator + ( PII u , int v ){
return Node( (u.first+v)%p.first , (u.second+v)%p.second );
}
bool operator != ( PII u,PII v ){
return !( u.first == v.first && u.second == v.second );
}
bool operator == ( PII u,PII v ){
return ( u.first == v.first && u.second == v.second );
}
PII Pow( PII a ,int b){
PII ans = One ;
while( b ){
if( b& )
ans = ans * a ;
b >>= ;
a = a * a ;
}
return ans ;
}
PII sum[N];
char str[N];
int ans[N];
int main()
{
ios_base :: sync_with_stdio();
cin.tie(NULL),cout.tie(NULL); while( cin >> str+ ){
if( str[] == '.') break;
int len = strlen(str+);
int n = len ;
sum[n+] = Zero ;
for(int i=len;i>=;i--)
sum[i] = sum[i+] * base + str[i] ;
int cnt = ;
/*
for(int i=1;i<=n;i++){
printf("%lld %lld \n",sum[i].first,sum[i].second);
}
*/
PII P = base ;
for(int i=n-;i>=;i--){
//printf("#### %d \n",i);
if( sum[] - sum[+i] == P * sum[n-i+] ){
ans[cnt++] = n-i ;
}
P = P * base ;
//printf("%lld * %lld = %lld \n ",sum[i].first,Pow(base,n-i-1).first ,(sum[n]-sum[n-i]).first );
}
//sort( ans , ans + cnt );
for(int i=;i<cnt;i++){
cout << ans[i] << ' ';
}
cout << n << endl;
//cout << ans << endl ;
}
return ;
}
双哈希