poj 2752 Seek the Name, Seek the Fame

来源:互联网 发布:中银淘宝信用卡怎么样 编辑:程序博客网 时间:2024/06/07 18:15

很基础的kmp题,只是需要将输出倒过来就行,于是用递归解决之。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int Maxn = 400010;char str[Maxn];int len, next[Maxn];bool flag;void get_next(){    int i = 0, j = -1;    next[i] = j;    while(i < len)    {        if(j == -1||str[i] == str[j])        {            i++, j++;            next[i] = j;        }        else            j = next[j];    }}void out(){    int tmp;    if(next[len] != -1)    {       tmp = next[len];       next[len] = next[next[len]];       flag = true;       out();    }    else        return;        if(tmp)        cout<<tmp<<" ";}int main(void){    while(scanf("%s",str) != EOF)    {      len = strlen(str);      get_next();      flag = false;      out();      cout<<len<<endl;    }    return 0;}


0 0
原创粉丝点击