【最长回文子串】【平衡树】[HDU5371]Hotaru's problem
来源:互联网 发布:linux磁盘挂载 编辑:程序博客网 时间:2024/05/17 07:00
题目
分析:用manacher求出以每个字符为中心的最长回文字串。
然后进行枚举。
若枚举到下标i,我们用两棵平衡树来存储在i前面,
#include<cstdio>#include<algorithm>#include<queue>#include<ctime>#include<cstring>#define MAXN 100000int b[MAXN*2+10],len[MAXN*2+10],ans,na,n,s[MAXN*2+10];int r[MAXN+10],nr,T,pre;using namespace std;void Read(int &x){ char c; while(c=getchar(),c!=EOF) if(c>='0'&&c<='9'){ x=c-'0'; while(c=getchar(),c>='0'&&c<='9') x=x*10+c-'0'; ungetc(c,stdin); return; }}void read(){ Read(n); na=n; int i; for(i=0;i<n;i++) Read(s[i]),r[i]=s[i]; sort(r,r+n); nr=unique(r,r+n)-r; for(i=n-1;i>=0;i--){ s[i]=lower_bound(r,r+nr,s[i])-r+1; s[i*2+2]=s[i],s[i*2+1]=-1; } s[n*2+2]=-2,s[0]=-3,s[n*2+1]=-1; na=n; n=(n+1)<<1;}struct node{ int val,pri; node *ch[2];}tree[MAXN*2+10],*tcnt=tree,*root1,*root2;void Rotate(node *&x,bool d){ node *y=x->ch[!d]; x->ch[!d]=y->ch[d]; y->ch[d]=x; x=y;}void insert1(node *&p,int val){ if(!p){ p=++tcnt; p->val=val; p->pri=rand(); p->ch[0]=p->ch[1]=0; return; } bool d=val>p->val; insert1(p->ch[d],val); if(p->ch[d]->pri<p->pri) Rotate(p,!d);}void insert2(node *&p,int val){ if(!p){ p=++tcnt; p->val=val; p->pri=rand(); p->ch[0]=p->ch[1]=0; return; } bool d=val+len[val]>=len[p->val]+p->val; insert2(p->ch[d],val); if(p->ch[d]->pri<p->pri) Rotate(p,!d);}void del(node *&p,int val){ if(p->val==val){ if(!p->ch[0]) p=p->ch[1]; else if(!p->ch[1]) p=p->ch[0]; else if(p->ch[0]->pri<p->ch[1]->pri){ Rotate(p,1); del(p->ch[1],val); } else{ Rotate(p,0); del(p->ch[0],val); } return; } if(val>p->val) del(p->ch[1],val); else del(p->ch[0],val);}void find_pre(node *p,int val){ if(!p) return; if(val>p->val) find_pre(p->ch[1],val); else if(val<p->val){ pre=min(pre,p->val); find_pre(p->ch[0],val); } else pre=val;}void bianli(node *&p,int i){ if(!p) return; bianli(p->ch[0],i); if(p->val+len[p->val]<i){ bianli(p->ch[1],i); del(root1,p->val); del(p,p->val); }}void solve(){ int i; for(i=1;i<na;i++){ bianli(root2,i); if(root1){ pre=0x7fffffff; find_pre(root1,i-len[i]); if(pre!=0x7fffffff) ans=max(ans,(i-pre)*3); } insert1(root1,i); insert2(root2,i); }}void manacher(){ int i,j,po=0,p=0; for(i=1;i<n;i++){ if(p>=i){ j=po*2-i; if(i+len[j]<=p){ len[i]=len[j]; continue; } else len[i]=p-i+1; } else len[i]=1; while(s[i+len[i]]==s[i-len[i]]) len[i]++; p=i+len[i]-1,po=i; } for(i=3;i<n;i+=2) len[i/2]=len[i]>>1;}int main(){ srand(time(0)); Read(T); int cnt=0; while(T--){ root1=root2=0,tcnt=tree; ans=0; read(); manacher(); solve(); printf("Case #%d: %d\n",++cnt,ans); }}
0 0
- 【最长回文子串】【平衡树】[HDU5371]Hotaru's problem
- hdu5371 Hotaru's problem 2015年多校第七场C题 最长回文串
- hdu5371 Hotaru's problem manachar回文串算法+枚举
- hdu5371 Hotaru's problem
- HDU5371 Hotaru's problem
- hdu5371 Hotaru's problem (Manacher)
- [Manacher] hdu5371 Hotaru's problem
- 【HDU5371】Hotaru's problem(Manacher + set)
- hdu5371(2015多校7)--Hotaru's problem(Manacher+线段树)
- hdu5371 Hotaru's problem(Manacher算法变形)
- hdu5371 Hotaru's problem(manacher 算法+枚举)
- Hotaru's problem(hdu5371+Manacher)多校7
- 字符串:HDU5371-Hotaru's problem(manacher 的应用)
- hdu5371 最长回文子串变形(Manacher算法)
- HDU 5371 Hotaru's problem(Manacher算法 回文串)
- HDU 5371(Hotaru's problem-2次回文串)
- leetcode Problem 5 最长子回文串
- HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
- Redis内存节省策略
- 反射错误汇总
- I/O接口标准(1):LVTTL、LVCMOS、SSTL、HSTL
- 用Messenger实现与被绑定的Service交互
- golang写的一套验证服务端以及web后台管理
- 【最长回文子串】【平衡树】[HDU5371]Hotaru's problem
- python核心编程-线程之threading模块
- Pasha and Stick
- 解读ASP.NET 5 & MVC6系列
- 用递归方法建立二叉树
- SMART Talk
- android Volley框架学习一
- LeetCode OJ ----Two Sum
- 【智能路由器】新手openwrt平台搭建