普通的KMP代码:
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
int nexts[10000],k;
void get_nexts(string t)
{nexts[0] = -1;int i = 0, j = -1;int len = t.size();while(i < len) {if(j == -1 || t[i] == t[j])nexts[++i] = ++j;elsej = nexts[j];}k=i;
}
bool KMP(string s,string t)
{get_nexts(t);int len1 = t.size();int len2 = s.size();int i = 0, j = 0;while( j < len2 ) {if( t[i] == s[j] ) {i++;j++;if( i == len1 ) {return true;}}else {i = nexts[i];if( i == -1 ){j++;i++;}}}return false;
}
int main()
{ sizeof(nexts,0,sizeof(nexts));string s,t;cin>>s>>t;cout<<KMP(s,t)<<endl;
return 0;
}
题目总结:
POJ 2752
POJ2406
POJ1961
POJ3461
HDU1711
HDU2087
HDU2203亲和串
HDU3746
HDU2594
HDU3336
HDU2328
正题:
POJ - 2752
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
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
int ne[500000];
void getnext(string k)
{ne[0]=-1;int i=0,j=-1;int len = k.size();while(i < len){if(j == -1 || k[i] == k[j]) {ne[++i] = ++j;}else {j = ne[j];}}
}
int main()
{ memset(ne,0,sizeof(ne));string s;int a[500000];while(cin >> s){ int n;n=0;getnext(s);int len = s.size();a[n++] = len;while(len){a[n++] = ne[len];len = ne[len];}n--;for(int i = n-1;i >= 0;i--) {if(i!=0) cout<<a[i]<<" ";else cout<<a[i]<<endl;}}
return 0;
}
/*有了kmp算法的next数组(保存某段子串的相同前后缀的最大长度,不包括自身),
可以从最长的相同前后缀开始考虑,对于串S,根据next[strlen(S)-1]得到的最长相
同前后缀为S1,则再根据next[strlen(S1)-1]得到的S1的最长相同前后缀S2肯定也为S
的相同前后缀。(通过画图可以很容易的证明)。*/
POJ2406
Given two strings a and b we define ab to be their concatenation. For example, if a = “abc” and b = “def” then ab = “abcdef”. If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = “” (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcd
aaaa
ababab
.
Sample Output
1
4
3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
int ne[1000010],k;
void getnext(string s,int len)
{ne[0] = -1;int i=0,j=-1;while(i < len) {if(j == -1 || s[i] == s[j]){ne[++i] = ++j;}else {j = ne[j];}}
}
int main()
{ IOS;string s;while(cin >> s && s != "." ) {int n = s.size();getnext(s,n);int m = n - ne[n];if(n%m==0) {cout<<(n/m)<<endl;}else {cout<< 1 <<endl;}}
return 0;
}
/*此题关键在于求出最长循环节的长度,假设一个字符串长
度为n,循环节长度为m,假设一个字符串是由K个子串组成的
,则字符串最后一个字符的NEXT数值一定是K-1个子串的长度,
所以只需求出NEXT[N]后N-NEXT[N]就是可能的最长循环节,但
是如果N-NEXT[N]不能整除N,则一定原字符串无循环,所以最
后模判定一下就行了*/
POJ1961
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.
Input
The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
number zero on it.
Output
For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
Sample Input
3
aaa
12
aabaabaabaab
0
Sample Output
Test case #1
2 2
3 3
Test case #2
2 2
6 2
9 3
12 4
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
int ne[1000010],k;
void getnext(string s,int m)
{ne[0] = -1;int i=0,j=-1;int len = m;while(i < len) {if(j == -1 || s[i] == s[j]){ne[++i] = ++j;}else {j = ne[j];}}
}
int main()
{ IOS;int k=1,m;string s;while(cin>>m&&m!=0) {cin>>s;cout<<"Test case #"<<k++<<endl;getnext(s,m);for( int i = 2;i <= m;i++) {int t = i - ne[i];if(i%t==0&&(i/t)!=1) {cout<<i<<" "<<(i/t)<<endl;}}cout<<endl;}
return 0;
}
/*这道题好像就是POJ 2406的加强版而已。那道题是输出
一个字符串的循环节出现的次数,这个是到第i个字符为止
,其实就是多了一层循环。把这个字符串遍历一次即可。*/
POJ3461
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ‘e’. He was a member of the Oulipo group. A quote from the book:
Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…
Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T’s is not unusual. And they never use spaces.
So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {‘A’, ‘B’, ‘C’, …, ‘Z’} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with the word W, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with |W| ≤ |T| ≤ 1,000,000.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
int ne[1000005],ans;
void getnext(string s,int len)
{ne[0] = -1;int i=0,j=-1;while(i < len){if(j == -1 || s[i] == s[j] ){ne[++i] = ++j;}else {j = ne[j];}}
}
void KMP(string t,string s, int lent,int lens)
{int i=0,j=0;while(i<lens) {if(j == -1 || s[i] == t[j]){i++;j++;if(j == lent) {ans++;j= ne[j];}}else {j = ne[j];}}
}
int main()
{ IOS;int k;cin>>k;string s,t;while(k--) {cin>>t>>s;ans=0;int lent = t.size();int lens = s.size();getnext(t,lent);KMP(t,s,lent,lens);cout<<ans<<endl;}
return 0;
}
HDU1711
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
int nexts[1000005],s[1000005],t[1000005],n,m;
void getnext()
{int i = 0,j = -1;nexts[0] = -1;while(i < m){if(j == -1 || t[i] == t[j]) {nexts[++i] = ++j;}else j = nexts[j];}
}
int kmp()
{ getnext();int i,j;i = j = 0;while(i < n){if(j == -1 || s[i] == t[j]){i++;j++;if(j == m){return i - m;}}else j = nexts[j];}return -1;
}
int main()
{ IOS;int T;cin>>T;while(T--) {cin>>n>>m;for(int i = 0;i < n;i++){cin>>s[i];}for(int i = 0;i < m;i++){cin>>t[i];}int ans = kmp();if(ans != -1) cout<<ans+1<<endl;else cout<<-1<<endl;}return 0;
}
HDU2087
(水题)
HDU2203亲和串
(把主串复制一遍接在主串后面,再进行kmp即可)
HDU3746
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
string t;
int nexts[100555];
void getnext()
{int len = t.size();int i = 0,j = -1;nexts[0] = -1;while(i < len){if(j == -1|| t[i] == t[j]){nexts[++i] = ++j;}else j = nexts[j];}
}
int main()
{ IOS;int n;cin>>n;while(n--){cin>>t;getnext();int len = t.size();if(nexts[len] == 0) cout<<len<<endl;else {if(len%(len - nexts[len]) == 0) cout<<0<<endl;else cout<<(len - nexts[len]) - len%(len - nexts[len])<<endl;}}return 0;
}
HDU2594
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
string s,t,ans;
int nexts[500055];
void getnext()
{int len = t.size();int i = 0,j = -1;nexts[0] = -1;while(i < len){if(j == -1|| t[i] == t[j]){nexts[++i] = ++j;}else j = nexts[j];}
}
void kmp()
{getnext();int i=0;int j=0;while( i < s.size() ){if(j == -1||s[i] == t[j]){i++;j++;}elsej=nexts[j];}if(!j)cout<<j<<endl;else{for(int k=0;k<j;k++)cout<<t[k];cout<<" "<<j<<endl;}
}
int main()
{ IOS;while(cin>>t>>s){kmp();}return 0;
}
*HDU3336
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
string t;
int nexts[200005];
void getnext()
{int len = t.size();int i = 0,j = -1;nexts[0] = -1;while(i < len){if(j == -1|| t[i] == t[j]){nexts[++i] = ++j;}else j = nexts[j];}
}
int main()
{ IOS;int T,len;cin>>T;while(T--){cin>>len>>t;getnext();int ans = len % 10007;for(int i = 1;i <= len;i++) if(nexts[i] != 0) ans = (ans+1) %10007;cout<<ans<<endl;}return 0;
}
HDU2328
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
int ne[1015];
void getnext(string s,int len)
{int i = 0;ne[0] = -1;int j = -1;while(i < len){if(j == -1 ||s[i] == s[j]){i++;j++;if(s[i] != s[j]) ne[i] = j;else ne[i] = ne[j];}else j = ne[j];}
}
int KMP(string t,string s, int lent,int lens)
{ getnext(t,lent);int i=0,j=0;while(i<lens) {if(j == -1 || s[i] == t[j]){i++;j++;if(j == lent) {return 1;}}else {j = ne[j];}}return 0;
}
int main()
{ IOS;int N;string s[4015];while(cin>>N && N){int k,len;len = inf;for(int i = 1;i <= N;i++){cin>>s[i];if(len > s[i].size()) {len = s[i].size();k = i;}}int flag = 0;string ans,ant;for(int i = len;i >= 1;i--){if(ans.size() > i) break;for(int j = 0; i + j <= len;j++){ant.clear();int h;for( h = j; h < i + j;h++){ant += s[k][h];}for( h = 1;h <= N;h++){if(h == k) continue;if(KMP(ant,s[h],ant.size(),s[h].size())) continue;else break;}if(h == N+1) {if(!flag) {ans = ant;flag = 1;}else {if(ans.size() > ant.size()) continue;else {if(ans > ant) ans = ant;}}}}}if(ans.size() == 0) cout<<"IDENTITY LOST"<<endl;else cout<<ans<<endl;}
return 0;
}