poj2752

来源:互联网 发布:淘宝客推广计划怎么写 编辑:程序博客网 时间:2024/05/22 18:03

发现不优化的next数组经常用啊……

#include<iostream>#include <string.h>#include <stdio.h>#include <stack>using namespace std;int next[1000010];char p[1000010];int pLen;void GetNext(){    pLen = strlen(p);    next[0] = -1;    int k = -1;    int j = 0;    while (j < pLen)    {        //p[k]表示前缀,p[j]表示后缀        if (k == -1 || p[j] == p[k])        {            ++j;            ++k;                next[j] = k;        }        else        {            k = next[k];        }    }}stack<int> s;int main(){    while(~scanf("%s",p)&&strcmp(p,".")!=0)    {        GetNext();       // cout<<pLen;        while(next[pLen]!=-1)        {            //cout<<pLen;            s.push(pLen);        pLen=next[pLen];        }       // return 0;        printf("%d",s.top());s.pop();        while(!s.empty())        {            printf(" %d",s.top());            s.pop();        }        printf("\n");    }    return 0;}