Passward (kmp)

来源:互联网 发布:淘宝的隐藏优惠券插件 编辑:程序博客网 时间:2024/06/03 19:53

Passward

9.11
思路:
kmp跑一边,从最后向前跳next,标记一下中间的串就行啦。

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>using namespace std;const int N = 100010;char s[N];int lens, nxt[N];bool flag[N];void calcnext() {    int i = 0, j = -1;    nxt[0] = -1;//    while(i < lens) {        if(j == -1 || s[i] == s[j]) {            i++, j++;            nxt[i] = j;        }        else            j = nxt[j];    }}int main(){    freopen ("passward.in", "r", stdin);    freopen ("passward.out", "w", stdout);    scanf("%s", s);    lens = strlen(s);    calcnext();    for(register int i=1; i<lens; i++) flag[nxt[i]] = true;//只有前缀和后缀的子串不会被标记     register int i;    for( i=nxt[lens]; i; i=nxt[i])//lens        if( flag[i] ) break;//越早break,串越长     if( !i ) printf("Just a legend");    else for(register int j=0; j<i; j++) putchar( s[j] );    return 0;}
原创粉丝点击