您的位置:首页 > 其它

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

inputoutput
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