HDU-4763

传送门

T组字符串 问每个字符串中是否能找到一个子串在原串中互不重叠地出现三次(或以上),且该子串是原串的前缀、后缀。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;

const int maxn = 1e6 + 10;
char s[maxn];
int f[maxn];
int len;
int T;
bool vis[maxn];

void getFail(char* P, int* f) {
    f[0] = 0; f[1] = 0;
    for (int i = 1; i < len; i++) {
        int j = f[i];
        while (j && P[i] != P[j]) j = f[j];
        f[i + 1] = P[i] == P[j] ? j + 1 : 0;
    }
}

int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%s", s);
        len = strlen(s);
        getFail(s, f);
        int t = len;
        int ans = 0;
        memset(vis, 0, sizeof(vis));
        while (t) {
            t = f[t];
            vis[t] = 1;
        }
        for (int i = len; i ; i--) {
            int t = i;
            while (t) {
                if (vis[f[t]] && t >= 2 * f[t] && len - t >= f[t]) {
                    ans = max(ans, f[t]);
                }
                t = f[t];
            }
        }
        printf("%d
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xFANx/p/8510900.html