题意

考虑对每个节点\(x\)维护\(lastpos_x\)表示\(x\)的所有后缀回文串中第一个\(len\leqslant len_x/2\)并且能和\(x\)最后一个字符匹配的,之后枚举节点,判断该点回文串是否合法。

求\(lastpos_x\):

如果新建节点\(x\)满足\(len_x\leqslant 2\),那么\(lastpos_x=fail_x\)。

不然则从\(x\)的父亲节点向上跳,边跳边判断即可。

code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=500010;
int n,ans;
char s[maxn];
struct PA
{
int last,tot;
int fail[maxn],len[maxn],lastpos[maxn];
int ch[maxn][30];
PA(){tot=1;fail[0]=1;len[1]=-1;}
inline int getfail(int x,int pos,char* s)
{
s[0]='#';
while(s[pos-len[x]-1]!=s[pos])x=fail[x];
return x;
}
inline void build(char* s,int n)
{
s[0]='#';
for(int i=1;i<=n;i++)
{
int c=s[i]-'a';
int p=getfail(last,i,s);
if(!ch[p][c])
{
int q=++tot;len[q]=len[p]+2;
int tmp=getfail(fail[p],i,s);
fail[q]=ch[tmp][c];ch[p][c]=q;
if(len[q]<=2)lastpos[q]=fail[q];
else
{
tmp=lastpos[p];
while(s[i-len[tmp]-1]!=s[i]||((len[tmp]+2)<<1)>len[q])tmp=fail[tmp];
lastpos[q]=ch[tmp][c];
}
}
last=ch[p][c];
}
}
}pa;
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
scanf("%d%s",&n,s+1);
pa.build(s,n);
for(int i=2;i<=pa.tot;i++)
if((pa.len[pa.lastpos[i]]<<1)==pa.len[i]&&pa.len[pa.lastpos[i]]%2==0)
ans=max(ans,pa.len[i]);
printf("%d",ans);
return 0;
}

最新文章

  1. Daily Scrum Meeting ——FifthDay(Beta)12.13
  2. jQuery取得select 选中值和文本 来自园友“大气象”
  3. 我总结的Android编程规范
  4. no2.crossdomain.xml批量读取(待完善)
  5. 转:C的|、||、&amp;、&amp;&amp;、异或、~、!运算
  6. ssm框架整合小结
  7. CentOS平台下为Python添加MongoDB支持PyMongo
  8. JVM经常使用的调优參数
  9. Windows7添加SSD硬盘后重启卡住正在启动
  10. HBase概念学习(七)HBase与Mapreduce集成
  11. sql还原(.sql文件还原)
  12. BSA Network Shell系列-nexec | runcmd | runscript | scriptutil的异同
  13. BZOJ_3675_[Apio2014]序列分割_斜率优化
  14. Linux防火墙iptables基础详解
  15. Python 的 GUI 开发工具
  16. Java ee第五周作业
  17. [Objective-C] 创建常量
  18. 002-打开文件管理规范-20190406.bat
  19. 【托业】【新托业TOEIC新题型真题】学习笔记8-题库五-&gt;P7
  20. 在Window下编译LibGeotiff(含Libtiff)

热门文章

  1. Flask 教程 第十一章:美化
  2. js-07-事件
  3. [转]UiPath Keyboard Shortcuts
  4. mongodb主备配置
  5. JAVA工程师技能要求
  6. C++标准库之string类型
  7. 【tf.keras】TensorFlow 1.x 到 2.0 的 API 变化
  8. robotframework框架 - 在Pycharm当中编写RobotFramework测试用例
  9. Github(第一次尝试)
  10. java超市购物管理系统