您的位置:首页 > 其它

HDOJ-2594 Simpsons’ Hidden Talents(KMP)

2016-03-13 20:02 393 查看
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;
#define maxn 50005
int next1[maxn<<1];
void KMP(string &s)
{
next1[0] = -1;
int j = -1, i = 0;

while(i < s.size())
{
if(j == -1 || s[i] == s[j])
{
i++;
j++;
next1[i] = j;
}
else
j = next1[j];
}
}
int main()
{
//  freopen("in.txt", "r", stdin);
string s1, s2;

while(cin >> s1 >> s2)
{
s1 += s2;
KMP(s1);
int t = next1[s1.size()];
if(t == 0)
cout << 0;
else
{
while(t > s1.size() - s2.size() || t > s2.size())
t = next1[t];
for(int i = 0; i < t; i++)
cout << s1[i];
cout << " " << t;
}
cout << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: