跳到正文

剪贴板

剪贴板 ybei2866

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <cstdio>
#include <cstring>
#define maxn 1000000
#define m1 10000007
#define m2 10000003
#define int long long

char s[maxn + 10];
int n, a[maxn + 10], hash1[2][maxn + 10], hash2[2][maxn + 10], maxl;
int chec[maxn + 10];

signed main(void) {
    scanf("%s", s + 1); n = strlen(s + 1);
    for (int i = 1; i <= n; i++)
    	a[i] = s[i] - 'a' + 1,
		hash1[0][i] = (hash1[0][i - 1] * 27 + a[i]) % m1,
		hash2[0][i] = (hash2[0][i - 1] * 27 + a[i]) % m2; //求前缀hash
    for (int i = 1, p1 = 1, p2 = 1; i <= n; i++, (p1 *= 27) %= m1, (p2 *= 27) %= m2)
    	hash1[1][i] = (hash1[1][i - 1] + p1 * a[n - i + 1] % m1) % m1,
    	hash2[1][i] = (hash2[1][i - 1] + p2 * a[n - i + 1] % m2) % m2; //求后缀hash
	for (int i = n - 1; i; i--)
		if (hash1[0][i] == hash1[1][i] && hash2[0][i] == hash2[1][i]) {
			int y = 1;
			for (int j = 1; j <= i; j++)
				if (a[j] != a[n - i + j]) {
					y = 0; break; //对比前后缀是否一样
				}
			if (y) {
				maxl = i; break;
			}
		}
	for (int i = 1; i + maxl <= n; i++)
		putchar(s[i]);
	printf("%s\n", s + 1);

    return 0;
}