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开头的最长回文串,然后枚 ...
随机推荐
- python-创建一个本地txt文本
def text_create(name, msg): desktop_path = '/Users/Hou/Desktop/' full_path = desktop_path + name + ' ...
- PHPExcel 对xls表格的文件读取
//先去http://phpexcel.codeplex.com/releases/view/119187 下载文件包最后解压Classes到当前目录header("Content-type ...
- Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案
Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案 1. 业务场景 android+webview h5 css背景图性能提升1 2. ...
- cocoa pods 升级遇到的问题
1. cocoa pods 升级遇到的问题 运行 sudo gem update --system 报错 Updating rubygems-update ERROR: While executing ...
- iOS图片上传及压缩
提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用.在这里,我们需要过UIImagePickerController类来和用户交互. 使用UIImagePickerContr ...
- [JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PWideChar
Delphi代码 unit UnitDll; interface uses StrUtils, SysUtils, Dialogs; function DoBusinessWide(pvData: P ...
- Github 创建新分支
一.clone Repository clone Github 上的Repository,如下: git clone git@github.com:FBing/design-patterns.git ...
- hdu 1000&hdu1001
1001 #include<iostream> #include<stdio.h> using namespace std; int main() { long long n; ...
- C# 一个长度为100的int数组,插入1-100的随机数,不能重复,如何写
int[] intArr = new int[100]; ArrayList myList = new ArrayList(); Random rnd = new Random(); while (m ...
- CentOS 7 ifconfig: command not found
# ifcon-bash: ifconfig: command not found谷歌了一下,整理了一下解决思路 查看ifconfig命令是否存在 查看 /sbin/ifconfig是否存在 如果if ...