URAL 2040 Palindromes and Super Abilities 2
2016-04-18 14:55
441 查看
Description
Dima adds letters s1, …, sn one by one to the end of a word. After each letter, he asks Misha to tell him how many new palindrome substrings appeared when he added that letter.
Two substrings are considered distinct if they are different as strings. Which n numbers will be said by Misha if it is known that he is never wrong?
Input
The input contains a string s1 … sn consisting of letters ‘a’ and ‘b’ (1 ≤ n ≤ 5 000 000).
Output
Print n numbers without spaces: i-th number must be the number of palindrome substrings of the prefix s1 … si minus the number of palindrome substrings of the
prefix s1 … si−1. The first number in the output should be one.
Sample Input
简单回文树应用,然而会卡输出,简直有毒
Dima adds letters s1, …, sn one by one to the end of a word. After each letter, he asks Misha to tell him how many new palindrome substrings appeared when he added that letter.
Two substrings are considered distinct if they are different as strings. Which n numbers will be said by Misha if it is known that he is never wrong?
Input
The input contains a string s1 … sn consisting of letters ‘a’ and ‘b’ (1 ≤ n ≤ 5 000 000).
Output
Print n numbers without spaces: i-th number must be the number of palindrome substrings of the prefix s1 … si minus the number of palindrome substrings of the
prefix s1 … si−1. The first number in the output should be one.
Sample Input
input | output |
---|---|
abbbba | 111111 |
Notes
We guarantee that jury has C++ solution which fits Time Limit at least two times. We do not guarantee that solution on other languages exists (even Java).简单回文树应用,然而会卡输出,简直有毒
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; typedef long long LL; const int low(int x) { return x&-x; } const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int maxn = 5e6 + 10; int T, n; char s[maxn], ans[maxn]; struct PalindromicTree { const static int maxn = 5e6 + 10; const static int size = 2; int next[maxn][size], last, sz, tot; int fail[maxn], len[maxn]; char s[maxn]; void clear() { len[1] = -1; len[2] = 0; fail[2] = fail[1] = 1; last = (sz = 3) - 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; 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]) { 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 1; } }solve; int main() { while (scanf("%s", s) != EOF) { solve.clear(); for (int i = 0; s[i]; i++) { ans[i] = solve.add(s[i]) + '0'; ans[i + 1] = 0; } puts(ans); } return 0; }
相关文章推荐
- 南京理工大学程序设计大赛(校外镜像)H 谁才是最强战舰!
- Nodejs4.x+express4.13.1入门 项目搭建及简单的路由控制
- 拼接符合highCharts格式的数据类型
- 使用按键精灵+umdh定位内存泄露问题的方式
- cocos笔记
- 深圳证券交易所网络投票业务指南
- poj 1036 Gangsters 简单dp
- bzoj 2162: 男生女生
- boost的配置解析库
- PHP常量与变量,函数
- 相似性度量(Similarity Measurement)
- OpenCV读取序列图片(二)
- 关于在不同品牌的手机在自带的存储空间上创建文件的区别
- 面向业务的立体化高可用架构设计
- iOS 单例模式 浅叙
- XAMPP(集成PHP+MySQL+Apache)修改Apache默认工程位置
- Win8.1安装itunes提示此windows installer软件包有一个问题如何解决
- Direct3D 11第三节 Shaders and Effect System
- OSG源码编译
- 华为研发实习生(一面)