HYSBZ3676-回文串
来源:互联网 发布:英语句子成分解析软件 编辑:程序博客网 时间:2024/06/09 07:05
[Apio2014]回文串
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 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
abacaba
【样例输入2]
www
Sample Output
【样例输出l】
7
【样例输出2]
4
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 longconst 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
- HYSBZ3676-回文串
- 回文数&&回文串
- 回文串
- 回文串
- 回文串
- 回文串
- 回文串
- 回文串
- 回文串
- 回文串
- 回文串
- 回文串
- 回文串
- **回文串
- 回文串
- 回文串
- 回文串
- 回文串
- DeepFace介绍
- Oracle数据库连接错误:ORA-12541:TNS:无监听程序
- 生成共用的一个Fragment
- linux 下maven安装
- 香港科技大学TensorFlow机器学习课程分享
- HYSBZ3676-回文串
- poj1852 Ants
- Docker服务网格的优点,微服务
- 【新国产化】中国云平台能像阿里云一样,成为世界一哥吗?
- hihocoder 1586 : Minimum ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛
- 多条目加载
- 我眼中的命令模式
- 前端之编辑器webstorm
- Node.js入门教程:Node.js如何安装配置并部署一个网站