2565: 最长双回文串

2565: 最长双回文串

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 1513   Solved: 777
[ Submit][ Status][ Discuss]

Description

顺序和逆序读起来完全一样的串叫做回文串。比如 acbca 是回文串,而 abc 不是( abc 的顺序为 “abc” ,逆序为 “cba” ,不相同)。
输入长度为 n 的串 S ,求 S 的最长双回文子串 T, 即可将 T 分为两部分 X Y ,( |X|,|Y|≥1 )且 X Y 都是回文串。

Input

一行由小写英文字母组成的字符串S

Output

一行一个整数,表示最长双回文子串的长度。

Sample Input

baacaabbacabb

Sample Output

12

HINT

样例说明

从第二个字符开始的字符串aacaabbacabb可分为aacaa与bbacabb两部分,且两者都是回文串。

对于100%的数据,2≤|S|≤10^5


2015.4.25新加数据一组

Source

[ Submit][ Status][ Discuss]


画个图,然后Manacher乱搞
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
using namespace std;

const int maxn = 2E5 + 10; 

int n,ans,p,len,t,r[maxn],L[maxn],R[maxn];
char a[maxn],b[maxn]; 

void Manacher(int po,int tot)
{
	bool flag = 1;
	for (;;) {
		if (b[po + tot] != b[po - tot]) break;
		++tot;
		if (po - tot < 0 || po + tot == len + 1) break;
	}
	r[po] = tot;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	scanf("%s",1 + a);
	len = strlen(a + 1);
	for (int i = 1; i <= len; i++) b[i*2 - 1] = a[i],b[i*2] = '#'; b[0] = '#';
	r[0] = 1; len *= 2; 
	for (int i = 1; i <= len; i++) {
		if (p + r[p] - 1 < i) Manacher(i,1),p = i;
		else {
			int j = 2*p - i;
			if (p - r[p] < j - r[j]) r[i] = r[j];
			else Manacher(i,p + r[p] - i),p = i;
		}
	}
	t = 0;
	for (int i = 1; i < len; i++) 
		if (i + r[i] - 1 > t) {
			for (int j = t + 1; j <= i + r[i] - 1; j++) 
				L[j] = j - i + 1;
			t = i + r[i] - 1;
		}
	t = len;
	for (int i = len - 1; i; i--) 
		if (i - r[i] + 1 < t) {
			for (int j = t - 1; j >= i - r[i] + 1; j--)
				R[j] = i - j + 1;
			t = i - r[i] + 1;
		}
	for (int i = 1; i < len - 1; i++) ans = max(ans,L[i] + R[i+1] - 1);
	cout << ans;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值