题解
题目大意 给你一个主串S 要求找到一个子串E满足S=EAEBE AB串可以为空
题目样例水了
使用kmp将next数组求出 vis数组记录中心子串满足的最长长度 记录的条件是如果以当前位置结尾长度为j作为子串E前缀和后缀有足够空间则记录 最后扫一遍vis得出答案
AC代码
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 10;
char s[MAXN], p[MAXN];
int nxt[MAXN]; //nxt子串[0, i)前后缀最长相等长度i - nxt[i]为字符串循环节长度
bool vis[MAXN];
void GetNext(const char *p, int m)
{
int i = 0, j = -1; //0号位为-1边界
nxt[0] = -1;
while (i < m)
if (!~j || p[i] == p[j]) //如果新增项相同或者回溯到边界则同时加
nxt[++i] = ++j;
else
j = nxt[j]; //如果新增项不同回溯到j对应的匹配终点
}
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int N;
cin >> N;
while (N--)
{
memset(vis, 0, sizeof(vis));
scanf("%s", s);
int n = strlen(s);
GetNext(s, n);
for (int i = 0; i < n; i++)
{
int j = nxt[i + 1]; //+1为当前位置为结尾的最长公共前后缀
while (j > 0 && !vis[j]) //如果处理过则不继续
{
if (j <= (i + 1) / 2 && j <= n - i - 1) //当前位置长度为j子串E前缀和后缀有足够空间
vis[j] = 1;
j = nxt[j]; //跳跃到下一个公共前后缀
}
}
int ans = 0, j = nxt[n];
while (j > 0 && !ans) //遍历vis标记
{
if (vis[j])
ans = j;
j = nxt[j];
}
cout << ans << endl;
}
return 0;
}