竞赛讨论区 > 玄学代码求解
头像
马丁丶德
发布于 2020-04-20 11:22
+ 关注

玄学代码求解

玄学代码不知道怎么过的,为啥要
for(int j = ne[cnt] ; j ; j = ne[j])????
#include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
const int N = 1000010;
char s[N],temp[N];
int ne[N],ne1[N];
 
void substr(int cnt)
{
    for(int i = 1; i <= cnt; i ++) temp[i] = s[i];
     
    for(int i = 2,j = 0; i <= cnt; i ++)
    {
        while(j && temp[i] != temp[j + 1]) j = ne1[j];
        if(temp[i] == temp[j + 1]) j ++;
        ne1[i] = j;
    }
}
 
int main()
{
    cin >> s + 1;
    int cnt = strlen(s + 1),x = 0;
    for(int i = 2,j = 0; s[i]; i ++)
    {
        while(j && s[i] != s[j + 1]) j = ne[j];
        if(s[i] == s[j + 1]) j ++;
        ne[i] = j;
    }
    bool flag = false;
    for(int j = ne[cnt]; j ; j = ne[j])
    {
        substr(j);
        for(int i = 2,k = 0; i <= cnt - 1; i ++)
        {
            while(k && s[i] != temp[k + 1]) k = ne1[k];
            if(s[i] == temp[k + 1]) k ++;
            if(k == j)
            {
                flag = true;
                break;
            }
        }
        if(flag == true) {x = j;break;}
    }
     
     
     
     
    if(!flag) puts("Just a legend");
    else
        for(int i = 1; i <= x;i ++) printf("%c",s[i]);
}


全部评论

(0) 回帖
加载中...
话题 回帖

本文相关内容

等你来战

查看全部

热门推荐