Description
https://www.luogu.org/problemnew/show/P3546
Solution
循环同构的前后缀一定可以表示成:AB...BA
我们设
f
i
f_i
fi表示去掉串长度为
i
i
i的前后缀后,最长的前后缀相同的部分。
性质:
f
i
≥
f
(
i
−
1
)
−
2
f_i\ge f(i-1)-2
fi≥f(i−1)−2
大概是酱紫的:
i-1:
x
x
x
a
b
c
d
e
…
a
b
c
d
e
x
x
x
xxx\color{red}{abcde} \dots\color{red}{abcde}\color{black}xxx
xxxabcde…abcdexxx
i:
x
x
x
x
b
c
d
…
b
c
d
x
x
x
x
xxxx\color{red}{bcd} \dots\color{red}{bcd}\color{black}xxxx
xxxxbcd…bcdxxxx
转化一下:
f
(
i
+
1
)
+
2
≥
f
(
i
)
f(i+1)+2\ge f(i)
f(i+1)+2≥f(i)
我们从大到小枚举
i
i
i,每次可以得到一个
f
(
i
)
f(i)
f(i)的上界,从上界开始枚举即可。
至于字符串对比,hash一下就行了。
Code
/************************************************
* Au: Hany01
* Date: Sep 26th, 2018
* Prob: bzoj2803 Prefixuffix
* Email: hany01dxx@gmail.com & hany01@foxmail.com
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 1e6 + 5, N = 1e6 + 5;
int n, t, ans, f[maxn];
char str[maxn];
struct Hash {
int base, MOD, Pow[maxn], val[maxn];
inline void getPow() {
Pow[0] = 1;
For(i, 1, N) Pow[i] = (LL)Pow[i - 1] * base % MOD;
}
inline void init() {
val[0] = 0;
For(i, 1, n) val[i] = ((LL)val[i - 1] * base + str[i]) % MOD;
}
inline bool check(int l1, int r1, int l2, int r2) {
return (val[r1] + MOD - (LL)val[l1 - 1] * Pow[r1 - l1 + 1] % MOD) % MOD == (val[r2] + MOD - (LL)val[l2 - 1] * Pow[r2 - l2 + 1] % MOD) % MOD;
}
}hash1, hash2;
inline bool check(int l1, int r1, int l2, int r2) {
if (l1 > r1) return 1;
return hash1.check(l1, r1, l2, r2) && hash2.check(l1, r1, l2, r2);
}
int main()
{
#ifdef hany01
freopen("bzoj2803.in", "r", stdin);
freopen("bzoj2803.out", "w", stdout);
#endif
hash1.base = 107, hash2.base = 41;
hash1.MOD = 1e9 + 7, hash2.MOD = 998244353;
hash1.getPow(), hash2.getPow();
n = read(), scanf("%s", str + 1);
t = n >> 1, hash1.init(), hash2.init();
f[t] = ans = 0;
Fordown(i, t - 1, 0) {
for (f[i] = min(f[i + 1] + 2, t - i); f[i] && !check(i + 1, i + f[i], n - i - f[i] + 1, n - i); -- f[i]);
if (check(1, i, n - i + 1, n)) chkmax(ans, i + f[i]);
}
printf("%d\n", ans);
return 0;
}