标签:while can return ring codeforce putchar lib || std
考虑使用\(kmp\)的\(nxt\)数组。我们先枚举\(i\)(除了\(1\)和\(n\)),将其对应的\(nxt\)打上标记,这样就意味着我们确定了条件中的前缀和中间的串。
我们再考虑后缀。可以从\(n\)开始,用\(nxt\)一个一个地跳。因为\(nxt\)本身就保证末尾至少相同。而每个\(nxt\)都保证是最长的,故正确。
#include<cstdio>
#include<cstdlib>
#include<cstring>
const int N = 1000010;
int n, nxt[N];
bool vis[N];
char s[N];
int read() {
int x = 0, f = 1; char s;
while((s = getchar()) > '9' || s < '0') {
if(s == '-') f = -1;
if(s == EOF) exit(0);
}
while(s >= '0' && s <= '9') {
x = (x << 1) + (x << 3) + (s ^ 48);
s = getchar();
}
return x * f;
}
void init() {
int j = 0;
for(int i = 2; i <= n; ++ i) {
while(j && s[i] != s[j + 1]) j = nxt[j];
if(s[i] == s[j + 1]) ++ j;
nxt[i] = j;
}
}
int main() {
scanf("%s", s + 1); n = strlen(s + 1);
init();
for(int i = 2; i < n; ++ i) vis[nxt[i]] = 1;
vis[0] = 0;
for(int i = n; i; i = nxt[i])
if(vis[nxt[i]]) {
for(int j = 1; j <= nxt[i]; ++ j) putchar(s[j]);
putchar('\n'); return 0;
}
puts("Just a legend");
return 0;
}
标签:while can return ring codeforce putchar lib || std
原文地址:https://www.cnblogs.com/AWhiteWall/p/12337012.html