您的位置:首页 > 其它

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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: