URAL 2040 Palindromes and Super Abilities 2
2015-10-27 13:05
453 查看
[b]Palindromes and Super Abilities 2[/b]
Time Limit: 500MS Memory Limit: 102400KB 64bit IO Format: %I64d & %I64u
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
abbbba
Sample Output
111111
Source
Problem Author: Mikhail Rubinchik (prepared by Kirill Borozdin)
解题:PalindromicTree
View Code
Time Limit: 500MS Memory Limit: 102400KB 64bit IO Format: %I64d & %I64u
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
abbbba
Sample Output
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).Source
Problem Author: Mikhail Rubinchik (prepared by Kirill Borozdin)
解题:PalindromicTree
#include <bits/stdc++.h> using namespace std; const int maxn = 5000010; struct PalindromicTree{ struct node{ int son[2],f,len; void init(int len){ memset(son,0,sizeof son); this->len = len; } }e[maxn]; int tot,last,n; char s[maxn]; int newnode(int len = 0){ e[tot].init(len); return tot++; } int getFail(int x){ while(s[n - e[x].len - 1] != s ) x = e[x].f; return x; } void init(){ last = tot = n = 0; newnode(0); newnode(-1); e[0].f = 1; s = -1; } bool extend(int c){ s[++n] = c; int cur = getFail(last); bool isExtend = e[cur].son[c] == 0; if(!e[cur].son[c]){ int newson = newnode(e[cur].len + 2); e[newson].f = e[getFail(e[cur].f)].son[c]; e[cur].son[c] = newson; } last = e[cur].son[c]; return isExtend; } }pt; char str[maxn]; int main(){ while(gets(str)){ pt.init(); for(int i = 0; str[i]; ++i){ if(pt.extend(str[i] - 'a')) putchar('1'); else putchar('0'); } putchar('\n'); } return 0; }
View Code
相关文章推荐
- eclipse-关于org.eclipse.wst.server.core
- hdoj 1269 迷宫城堡
- 组件化开发
- [胡策题] 魔法水晶 树上问题~(四星)
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
- 两台机器间libevent通信:No route to host问题
- tnsnames.ora中UR=A配置使用
- php开启openssl
- 【FastDev4Android框架开发】Android实现沉浸式状态栏(六)
- ORACLE RAC ASM环境下DATAGUARD搭建步骤
- 【ACM】1020:迭代法
- VisualVM 性能调优过程(测试demo)
- VMware扩大Linux虚拟机硬盘空间
- java面试:兔子问题
- 南阳理工 括号配对题解
- Maven和Ant的区别
- Java内部类的调用
- LeetCode(147) Insertion Sort List
- MongoDB(四)之副本集
- windows和linux下简单的软件级调整相机曝光时间(或其他参数)的方式