HYSBZ 2565 最长双回文串 (回文树)
2565: 最长双回文串
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1377 Solved: 714
[Submit][Status][Discuss]
Description
输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(|X|,|Y|≥1)且X和Y都是回文串。
Input
一行由小写英文字母组成的字符串S。
Output
Sample Input
Sample Output
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
typedef long long int LL;
const int maxn=1e5+5;
char str[maxn];
struct Tree
{
int next[maxn][26];
int fail[maxn];
int len[maxn];
int s[maxn];
int last;
int p;
int n;
int new_node(int x)
{
memset(next[p],0,sizeof(next[p]));
len[p]=x;
return p++;
}
void init()
{
p=0;
new_node(0);
new_node(-1);
last=0;n=0;
s[0]=-1;
fail[0]=1;
}
int get_fail(int x)
{
while(s[n-len[x]-1]!=s[n])
x=fail[x];
return x;
}
int add(int x)
{
x-='a';
s[++n]=x;
int cur=get_fail(last);
if(!(last=next[cur][x]))
{
int now=new_node(len[cur]+2);
fail[now]=next[get_fail(fail[cur])][x];
next[cur][x]=now;
last=now;
}
return len[last];
}
}tree;
int s1[maxn];
int s2[maxn];
int main()
{
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
tree.init();
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
for(int i=0;i<len;i++)
{
s1[i]=tree.add(str[i]);
}
tree.init();
for(int i=len-1;i>=0;i--)
{
s2[i]=tree.add(str[i]);
}
int ans=0;
for(int i=0;i<len;i++)
{
ans=max(ans,s1[i]+s2[i+1]);
}
printf("%d\n",ans);
}
return 0;
}
HYSBZ 2565 最长双回文串 (回文树)的更多相关文章
- BZOJ 2565: 最长双回文串 [Manacher]
2565: 最长双回文串 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1842 Solved: 935[Submit][Status][Discu ...
- bzoj 2565: 最长双回文串 manacher算法
2565: 最长双回文串 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem. ...
- 2565: 最长双回文串 - BZOJ
Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同). 输入长度为n的串S,求S的最长双回文子串T ...
- bzoj 2565: 最长双回文串
Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为"abc",逆序为"cba",不相同).输入 ...
- bzoj 2565: 最长双回文串 回文自动机
题目: Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为"abc",逆序为"cba",不相同 ...
- BZOJ 2565 最长双回文串(回文自动机)
题意 给一个长度为N的字符串S.对于一个字符串AB,如果A和B都是回文串,那么称AB是一个双回文串.求问S最长双回文子串的长度?N <= 100000 题解 正反双向构造回文自动机,得到某一个点 ...
- BZOJ 2565 最长双回文串(manacher)
565: 最长双回文串 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3343 Solved: 1692[Submit][Status][Discu ...
- Manacher || P4555 [国家集训队]最长双回文串 || BZOJ 2565: 最长双回文串
题面:P4555 [国家集训队]最长双回文串 题解:就.就考察马拉车的理解 在原始马拉车的基础上多维护个P[i].Q[i]数组,分别表示以i结尾最长回文子串的长度和以i开头的最长回文子串的长度 然后就 ...
- 【BZOJ】2565: 最长双回文串
[题意]给定小写字母字符串s,求最长的 [ 可以分成左右两个回文串的 ] 子串,n<=10^5. [算法]回文树 [题解]对于每个字符x,处理出以x结尾的最长回文串,以x开头的最长回文串,然后枚 ...
随机推荐
- C#定时任务的偷懒实现
通常会有些定时任务的工作,例如每分钟统计一下xxx用户的xxx数量 或者 定时拉取下数据 之类的任务. 通常要实现定时调度功能和控制线程是否可以并发执行. 所以通常一个简单的小项目搞成大项目,但是使用 ...
- jQuery源代码学习:经常使用正則表達式
转载自:http://nuysoft.iteye.com/blog/1217898 经常使用的数字正则(严格匹配) 正则 含义 ^[1-9]\d*$ 匹配正整数 ^-[1-9]\d*$ 匹配负整数 ^ ...
- centos7下安装openvpn,访问内网服务器 (三)证书取消授权
1.创建临时证书 使用easy-rsa创建额外的证书: [root@origalom openvpn]# cd /usr/share/easy-rsa/2.0/ [root@origalom 2.0] ...
- [svc][jk]监控jvm的一个坑
监控jvm的一个坑 1,遇到的问题 我按照以往文档,在catalina.sh里追加jvm的监控api,如下 紧接着我启动 tomcat. 未报任何错误. 发现 lsof –i:12000, 12000 ...
- (1)Smali系列学习之Smali函数调用语句分析
一.函数调用smali中的函数和成员变量也分为两种,分别为 direct 和 virtual.两者的区别如下: 1.direct method 是指调用private方法.2.virtual meth ...
- Yii2数据库查询语法
一: $con = Yii::$app->db; $rel = $con->createCommand("select * from user");//预处理对象 $r ...
- 3. Digit Counts【medium】
Count the number of k's between 0 and n. k can be 0 - 9. Example if n = 12, k = 1 in [0, 1, 2, 3, ...
- windows编译tensorflow c++库
1. 准备 windows 10系统.3.6GHz cpu.16G 内存 visual studio 2017 or 2015 下载安装git 下载安装cmake 下载安装swigwin 如果不需要p ...
- VMWare虚拟机端口转发
# VMWare虚拟机端口转发 虚拟机和宿主机共用主机IP对外提供服务,又不想提供给虚拟机独立IP,使用VM虚拟机提供的端口转发功能正合适. ### 端口转发--------------------- ...
- 简单而直接的Python web 框架:web.py
web.py 是一个Python 的web 框架,它简单而且功能强大.web.py 是公开的,无论用于什么用途都是没有限制的. 先让大家感受一下web.py 的简单而强大: import web ur ...