这道题目当时做的时候先用manacher处理过,但是之后的求法太有问题了,所以一直WA。
我们可以发现,题目中所给的三部分,其实就是两个回文子串,我们这样定义这个N-sequence
。。。i。。。j。。。,把i当做第一个回文子串的中心(用mancher处理过后,相当于这里是另外新加的一个数字),j为第二个回文子串的中心,那么我们先将i+p[i] (p[i]为以i为中心的回文子串半径),那么从i+p[i]位置往前找,找到一个j,是的j-i>=len[j],这样子,i与j之间所夹的那个子串就是在i左边和j右边共用的回文子串中的一般,此时三部分中的一部分的长度就为(j-i)/2 (除以2是因为用manacher处理过后,有一半的数字是自己加上去的),然后最后再乘以3即可。
这里需要注意的几个点:
1->: 枚举暴力分割点时,下标i对应的字符必须是#。
2->:初始化j时j=i+p[i]-1,而不是j=i+[i]。因为i+p[i]-1才是对应的字符#。
3->:ans=j-i;而不是ans=(j-i)/2*3;因为后面还会继续和ans比较
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=100005;
int p[maxn*2];
int s[maxn],s_new[maxn*2];
int len;
//建立辅助数组
void init(){
s_new[0]=-INF;
s_new[1]=INF;
int cnt=1;
for(;cnt<=len;cnt++){
s_new[cnt*2]=s[cnt];
s_new[cnt*2+1]=INF;
}
s_new[cnt*2]=INF;
return ;
}
//马拉车算法
void manacher(){
int Len=len*2+2;
int mid=0,r=0;
for(int i=1;i<Len;i++){
if(i<r)p[i]=min(p[2*mid-i],r-i);
else p[i]=1;
while(s_new[i+p[i]]==s_new[i-p[i]])p[i]++;
if(r<i+p[i]){
r=i+p[i];
mid=i;
}
}
}
int main(){
int t,ans,Case=0;
scanf("%d",&t);
while(t--){
scanf("%d",&len);
for(int i=1;i<=len;i++){
scanf("%d",&s[i]);
}
init();//建立辅助数组
manacher();//manacher算法 求数组p[]
ans=0;
for(int i=1;i<len*2+2;i+=2){
for(int j=i+p[i]-1;j-i>ans;j-=2){
if(j-i+1<=p[j]){
ans=j-i;
break;
}
}
}
printf("Case #%d: %d\n",++Case,ans/2*3);
memset(p,0,sizeof(p));
}
return 0;
}
// /\ | / |**、
// / \ | / | \
// / \ |/ | / _____ ____ | /
// /------\ |\ |__/ / \ \ /\ / / \ | /
// / \ | \ | / \ \ / \ / /______\ |/
// / \ | \ | \ / \ / \ / \ |
// / \ | \ | \_____/ \/ \/ \_____ |
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//