HYSBZ3676-回文串

[Apio2014]回文串

Time Limit: 20 Sec   Memory Limit: 128 MB
Submit: 2843   Solved: 1265
[ Submit][ Status][ Discuss]

Description

考虑一个只包含小写拉丁字母的字符串s。我们定义s的一个子串t的“出 
现值”为t在s中的出现次数乘以t的长度。请你求出s的所有回文子串中的最 
大出现值。 

Input

输入只有一行,为一个只包含小写字母(a -z)的非空字符串s。 

Output


输出一个整数,为逝查回文子串的最大出现值。 

Sample Input

【样例输入l】
abacaba

【样例输入2]
www

Sample Output

【样例输出l】
7

【样例输出2]
4

HINT



一个串是回文的,当且仅当它从左到右读和从右到左读完全一样。 

在第一个样例中,回文子串有7个:a,b,c,aba,aca,bacab,abacaba,其中: 

● a出现4次,其出现值为4:1:1=4 

● b出现2次,其出现值为2:1:1=2 

● c出现1次,其出现值为l:1:l=l 

● aba出现2次,其出现值为2:1:3=6 

● aca出现1次,其出现值为1=1:3=3 

●bacab出现1次,其出现值为1:1:5=5 

● abacaba出现1次,其出现值为1:1:7=7 

故最大回文子串出现值为7。 

【数据规模与评分】 

数据满足1≤字符串长度≤300000。

Source


解题思路:回文树


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

const int maxn = 3e5 + 10;
char s[maxn];

struct PalindromicTree
{
	const static int maxn = 3e5 + 10;
	int next[maxn][26], last, sz, tot;
	int fail[maxn], len[maxn];
	LL cnt[maxn];
	char s[maxn];
	void Clear()
	{
		len[1] = -1; len[2] = 0;
		fail[2] = fail[1] = 1;
		last = (sz = 3) - 1;
		cnt[1] = cnt[2] = tot = 0;
		memset(next[1], 0, sizeof(next[1]));
		memset(next[2], 0, sizeof(next[2]));
	}
	int Node(int length)
	{
		memset(next[sz], 0, sizeof(next[sz]));
		len[sz] = length, cnt[sz] = 0;
		return sz++;
	}
	int getfail(int x)
	{
		while (s[tot] != s[tot - len[x] - 1]) x = fail[x];
		return x;
	}
	int add(char pos)
	{
		int x = (s[++tot] = pos) - 'a', y = getfail(last);
		if (next[y][x]) { ++cnt[last = next[y][x]]; return 0; }
		last = next[y][x] = Node(len[y] + 2);
		fail[last] = len[last] == 1 ? 2 : next[getfail(fail[y])][x];
		return ++cnt[last], 1;
	}
	void work()
	{
		LL ans = 0;
		for (int i = sz - 1; i > 2; i--)
		{
			cnt[fail[i]] += cnt[i];
			ans = max(ans, cnt[i] * len[i]);
		}
		printf("%lld\n", ans);
	}
}solve;

int main()
{
	while (~scanf("%s", s))
	{
		solve.Clear();
		for (int i = 0; s[i]; i++) solve.add(s[i]);
		solve.work();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值