标签:
1 10 2 3 4 4 3 2 2 3 4 4
Case #1: 9
先匹配第一个,再暴力找第二个,
注意只找比当前答案优的优化速度
#include<bits/stdc++.h> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (300000+10) #define Sp_char1 (-1) #define Sp_char2 (-2) typedef long long ll; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} int a[MAXN],n,r[MAXN]; class manacher { public: int n; int s[MAXN]; int p[2*MAXN+2]; void mem(){n=0; MEM(s) MEM(p)} int str[MAXN*2+2]; void work() { str[0]=Sp_char1; Rep(i,n) str[2*i+1]=Sp_char2,str[2*i+2]=s[i]; str[2*n+1]=Sp_char2; str[2*n+2]=-3; n=2*n+2; MEM(p) int mx=0,id=0; For(i,n-1) { if (i<mx) p[i]=min(p[2*id-i],mx-i); while(str[i-p[i]]==str[i+p[i]]) ++p[i]; if (mx<i+p[i]) //mx为已查明的最右端 { mx=i+p[i]; id=i; } } } }S; int main() { // freopen("C.in","r",stdin); int T;cin>>T; For(kcase,T) { S.mem(); scanf("%d",&n); For(i,n) scanf("%d",&a[i]),S.s[i-1]=a[i]; S.n=n; S.work(); int ans=0; for(int i=3;i<S.n;i+=2) { for(int j=S.p[i];j>ans;j-=2) { if (S.p[i+(j-1)]>=j) ans=max(ans,j); } } ans=(ans-1)/2*3; printf("Case #%d: %d\n",kcase,ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5371(Hotaru's problem-2次回文串)
标签:
原文地址:http://blog.csdn.net/nike0good/article/details/48009409