【hash】Seek the Name, Seek the Fame
【哈希和哈希表】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:)
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的。
1 #include<bits/stdc++.h> 2 #define Mp make_pair 3 #define F first 4 #define S second 5 using namespace std; 6 const int N = 1e6+7; 7 const int M1 = 1e9+7 , M2 = 1e9+9; 8 typedef long long ll; 9 typedef struct Node{ 10 long long first ,second; 11 12 Node (){} 13 Node ( ll u,ll v){ first = u , second = v ;} 14 }PII; 15 //typedef pair<ll,ll> PII; 16 17 const PII base{M2,M1},p{M1,M2},One{1ll,1ll},Zero{0ll,0ll}; 18 19 PII operator - (PII u,PII v){ 20 return Node( (u.first-v.first+p.first)%p.first ,(u.second-v.second+p.second)%p.second ); 21 } 22 PII operator * ( PII u , PII v ){ 23 return Node( (u.first*v.first)%p.first , (u.second*v.second)%p.second ); 24 } 25 PII operator + ( PII u , PII v ){ 26 return Node( (u.first+v.first)%p.first , (u.second+v.second)%p.second ); 27 } 28 PII operator + ( PII u , int v ){ 29 return Node( (u.first+v)%p.first , (u.second+v)%p.second ); 30 } 31 bool operator != ( PII u,PII v ){ 32 return !( u.first == v.first && u.second == v.second ); 33 } 34 bool operator == ( PII u,PII v ){ 35 return ( u.first == v.first && u.second == v.second ); 36 } 37 PII Pow( PII a ,int b){ 38 PII ans = One ; 39 while( b ){ 40 if( b&1 ) 41 ans = ans * a ; 42 b >>= 1 ; 43 a = a * a ; 44 } 45 return ans ; 46 } 47 PII sum[N]; 48 char str[N]; 49 int ans[N]; 50 int main() 51 { 52 ios_base :: sync_with_stdio(0); 53 cin.tie(NULL),cout.tie(NULL); 54 55 while( cin >> str+1 ){ 56 if( str[1] == '.') break; 57 int len = strlen(str+1); 58 int n = len ; 59 sum[n+1] = Zero ; 60 for(int i=len;i>=1;i--) 61 sum[i] = sum[i+1] * base + str[i] ; 62 int cnt = 0 ; 63 /* 64 for(int i=1;i<=n;i++){ 65 printf("%lld %lld \n",sum[i].first,sum[i].second); 66 } 67 */ 68 PII P = base ; 69 for(int i=n-1;i>=1;i--){ 70 //printf("#### %d \n",i); 71 if( sum[1] - sum[1+i] == P * sum[n-i+1] ){ 72 ans[cnt++] = n-i ; 73 } 74 P = P * base ; 75 //printf("%lld * %lld = %lld \n ",sum[i].first,Pow(base,n-i-1).first ,(sum[n]-sum[n-i]).first ); 76 } 77 //sort( ans , ans + cnt ); 78 for(int i=0;i<cnt;i++){ 79 cout << ans[i] << ' '; 80 } 81 cout << n << endl; 82 //cout << ans << endl ; 83 } 84 return 0; 85 } 86