URAL1960-Palindromes and Super Abilities
2017-10-10 19:10
323 查看
Palindromes and Super Abilities
Time limit: 1.0 secondMemory limit: 64 MB
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 ≤ 105).
Output
Output n numbers separated by whitespaces, i-th of these numbers must be the number of different nonempty substrings of prefix s1...si thatare palindromes.
Sample
input | output |
---|---|
aba | 1 2 3 |
Problem Source: Ural FU contest. Kontur Cup. Petrozavodsk training camp. Winter 2013
题意:给你一个字符串,求出每个前缀有多少种回文串
解题思路:回文树,回文树每开一个新节点就是一种新的回文串
#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];
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 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))
{
solve.Clear();
int ans = 0, flag = 0;
for (int i = 0; s[i]; i++)
{
ans += solve.add(s[i]);
if (flag) printf(" ");
else flag = 1;
printf("%d", ans);
}
printf("\n");
}
return 0;
}
相关文章推荐
- URAL 1960 Palindromes and Super Abilities
- 【后缀回文自动机】 URAL 1960 Palindromes and Super Abilities
- ural 1960 Palindromes and Super Abilities 题解
- 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
- URAL 1960 Palindromes and Super Abilities (回文树)
- 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
- URAL 1960- Palindromes and Super Abilities (回文树)
- URAL - 1960 Palindromes and Super Abilities
- URAL 1960 Palindromes and Super Abilities (Palindromic Tree)
- [ural 1960] Palindromes and Super Abilities
- URAL1960 Palindromes and Super Abilities
- 【ural】1960. Palindromes and Super Abilities【Palindromic Tree】
- 【URAL】1960. Palindromes and Super Abilities
- Ural1960 Palindromes and Super Abilities
- Ural 1960 Palindromes and Super Abilities
- Ural1960 Palindromes and Super Abilities
- Ural 2040. Palindromes and Super Abilities 2 回文自动机
- URAL2040-Palindromes and Super Abilities 2
- URAL 2040 Palindromes and Super Abilities 2
- Ural 2040 Palindromes and Super Abilities 2 回文自动机