HDOJ-2594 Simpsons’ Hidden Talents(KMP)

来源:互联网 发布:印度软件 编辑:程序博客网 时间:2024/06/05 08:21
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <string>using namespace std;#define maxn 50005int 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;}
0 0
原创粉丝点击