URAL 1960 Palindromes and Super Abilities
2016-04-13 13:57
323 查看
Description
After solving seven problems on Timus Online Judge with a word “palindrome” in the problem name, Misha has got an unusual ability. Now, when he reads a word, he can mentally count the number of unique nonempty substrings of this
word that are palindromes.
Dima wants to test Misha’s new ability. He adds letters s1, ..., sn to a word, letter by letter, and after every letter asks Misha, how many different nonempty palindromes current
word contains as substrings. Which n numbers will Misha say, if he will never be wrong?
Input
The only line of input contains the string s1... sn, where si are small English letters (1 ≤ n ≤ 10 5).
Output
Output n numbers separated by whitespaces, i-th of these numbers must be the number of different nonempty substrings of prefix s1... sithat are palindromes.
Sample Input
回文树模板题,练练手
After solving seven problems on Timus Online Judge with a word “palindrome” in the problem name, Misha has got an unusual ability. Now, when he reads a word, he can mentally count the number of unique nonempty substrings of this
word that are palindromes.
Dima wants to test Misha’s new ability. He adds letters s1, ..., sn to a word, letter by letter, and after every letter asks Misha, how many different nonempty palindromes current
word contains as substrings. Which n numbers will Misha say, if he will never be wrong?
Input
The only line of input contains the string s1... sn, where si are small English letters (1 ≤ n ≤ 10 5).
Output
Output n numbers separated by whitespaces, i-th of these numbers must be the number of different nonempty substrings of prefix s1... sithat are palindromes.
Sample Input
input | output |
---|---|
aba | 1 2 3 |
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<bitset> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<functional> 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 = 1e5 + 10; int T, n; char s[maxn]; struct PalindromicTree { const static int maxn = 1e5 + 10; const static int size = 26; int next[maxn][size], last; int fail[maxn], len[maxn], sz, tot; char s[maxn]; void clear() { len[1] = -1; len[2] = 0; fail[2] = fail[1] = 1; last = (sz = 3) - 1; tot = 0; memset(next[1], 0, sizeof(next[1])); memset(next[2], 0, sizeof(next[2])); } int add(char pos) { int x = (s[++tot] = pos) - 'a'; int y = last, z = len[last]; while (true) { if (tot - 1 - z > 0 && pos == s[tot - 1 - z]) break; y = fail[y]; z = len[y]; } if (next[y][x]) { last = next[y][x]; return 0; } last = next[y][x] = sz; len[sz] = len[y] + 2; memset(next[sz], 0, sizeof(next[sz])); if (len[sz] == 1) { fail[sz++] = 2; return 1; } while (true) { y = fail[y]; z = len[y]; if (tot - 1 - z > 0 && pos == s[tot - 1 - z]) break; } fail[sz++] = next[y][x]; return 1; } }solve; int main() { while (scanf("%s", s) != EOF) { solve.clear(); int ans = 0; for (int i = 0; s[i]; i++) { ans += solve.add(s[i]); printf("%s%d", i ? " " : "", ans); } putchar(10); } return 0; }
相关文章推荐
- Ural 1910. Titan Ruins: Hidden Entrance
- URAL - 2018 The Debut Album
- Ural 1068
- 【URAL刷题记】 URAL 1090~URAL 1097
- URAL 1091 Tmutarakan Exams
- URAL 1533 Fat Hobbits
- 【URAL 刷题记】URAL 1600 ~ URAL 1607
- 【URAL 刷题记】URAL 1317 ~ URAL 1326
- 【URAL 刷题记】URAL 1028 ~ URAL 1035
- Ural 1223.Chernobyl’ Eagle on a Roof
- ural 1078.Srgments
- ural 1013. K-based Numbers. Version 3
- ural1100--Final Standings--数据结构--结构体排序
- Ural 1057 Amount of Degrees(数位DP)
- ural 1869. New Year Cruise
- URAL 1005 Stone Pile
- URAL 1009
- URAL 1057 数位DP
- UVA - 10780 Again Prime? No Time.
- URAL 1019 - Line Painting(线段树)