题解
就是一个回文串拼上左右两端
类似二分找lcp这么做
可以直接用哈希找回文串
注意要找A串前半部分,B串找后半部分
代码
#include <bits/stdc++.h>
#define enter putchar('
')
#define space putchar(' ')
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define MAXN 100005
#define mo 99994711
#define pb push_back
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int N;
char strA[MAXN],strB[MAXN];
int A[MAXN],B[MAXN],rev_A[MAXN],rev_B[MAXN],e[MAXN];
int mul(int a,int b) {
return 1LL * a * b % mo;
}
int inc(int a,int b) {
return a + b >= mo ? a + b - mo : a + b;
}
int check1(int *a,int l,int r) {
return mul(inc(a[r],mo - a[l - 1]),e[N - l]);
}
int check2(int *a,int l,int r) {
return mul(inc(a[l],mo - a[r + 1]),e[r - 1]);
}
int Find(int *f,int *b,int s,int t) {
if(s < 1 || t > N) return 0;
int L = 0,R = min(N - t + 1,s);
while(L < R) {
int mid = (L + R + 1) >> 1;
if(check1(f,s - mid + 1,s) == check2(b,t,t + mid - 1)) L = mid;
else R = mid - 1;
}
return L;
}
void update(int &x,int y) {
x = max(x,y);
}
void Solve() {
read(N);
scanf("%s%s",strA + 1,strB + 1);
e[0] = 1;
for(int i = 1 ; i <= N ; ++i) {
e[i] = mul(e[i - 1],47);
}
for(int i = 1 ; i <= N ; ++i) {
A[i] = mul(e[i - 1],strA[i] - 'A' + 1);
A[i] = inc(A[i],A[i - 1]);
B[i] = mul(e[i - 1],strB[i] - 'A' + 1);
B[i] = inc(B[i],B[i - 1]);
}
for(int i = N ; i >= 1 ; --i) {
rev_A[i] = mul(e[N - i],strA[i] - 'A' + 1);
rev_A[i] = inc(rev_A[i],rev_A[i + 1]);
rev_B[i] = mul(e[N - i],strB[i] - 'A' + 1);
rev_B[i] = inc(rev_B[i],rev_B[i + 1]);
}
int ans = 1;
for(int i = 2 ; i <= N - 1; ++i) {
int L = Find(A,rev_A,i - 1,i + 1);
int t = Find(A,rev_B,i - L - 1,i + L);
update(ans,2 * L + 2 * t + 1);
}
for(int i = 1 ; i <= N - 1 ; ++i) {
int L = Find(A,rev_A,i,i + 1);
int t = Find(A,rev_B,i - L,i + L);
update(ans,2 * L + 2 * t);
}
for(int i = 2 ; i <= N - 1; ++i) {
int L = Find(B,rev_B,i - 1,i + 1);
int t = Find(A,rev_B,i - L,i + L + 1);
update(ans,2 * L + 2 * t + 1);
}
for(int i = 1 ; i <= N - 1; ++i) {
int L = Find(B,rev_B,i,i + 1);
int t = Find(A,rev_B,i - L + 1,i + L + 1);
update(ans,2 * L + 2 * t);
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}