标签:space class system lan i++ main max oid hdu
#include<iostream>
#include<string>
using namespace std;
void getNext(string s, int *nex)
{
nex[0] = -1;
int i = 0, j = -1;
int len = s.length();
while(i < len)
{
if(j == -1 || s[i] == s[j])
{
i++, j++;
nex[i] = j;
}
else j = nex[j];
}
}
int max(int a, int b) { return a > b ? a : b;}
int KMP(string s1, string s2, int *nex)
{
int len1 = s1.length();
int i = 0, j = 0;
while(i < len1)
{
if(s1[i] == s2[j] || j == -1)
i++, j++;
else j = nex[j];
}
return max(j-1, 0);
}
int main()
{
string s1, s2;
while(getline(cin, s1))
{
int nex[60000];
getline(cin, s2);
//cout << s2 << endl;
getNext(s1, nex);
int len = KMP(s2, s1, nex);
if(len == 0) printf("0\n");
else
{
for(int i = 0; i < len+1; i++) printf("%c", s1[i]);
printf(" %d\n", len+1);
}
}
system("pause");
return 0;
}
HDU 2594 Simpsons’ Hidden Talents KMP
标签:space class system lan i++ main max oid hdu
原文地址:https://www.cnblogs.com/znk97/p/14043952.html