Palindromes and Super abilities 2
Time Limit: 1MS |
|
Memory Limit: 102400KB |
|
64bit IO Format: %i64d &%i64u |
Status
Description
Dima adds letters
s1,...,
s
NOne by one to the end of a word. After all, he asks Misha to tell him how many new palindrome substrings appeared when he added the letter. The substrings is considered distinct if they is different as strings. which
NNumbers'll be said by Misha if it's known that he's never wrong?
Input
The input contains a string
s1 ...
s
NConsisting of letters ' a ' and ' B ' (1≤
N≤5 000 000).
Output
Print
NNumbers without spaces:
I-th number must be the number of palindrome substrings of the prefix
s1 ...
s
IMinus the number of palindrome substrings of the prefix
s1 ...
s
I? 1. The first number in the output should is one.
Sample Input
input |
Output |
Abbbba |
111111 |
Noteswe guarantee that jury have C + + solution which fits time Limit at least. We do not guarantee this solution on other languages exists (even Java).
Source
problem Author:Mikhail Rubinchik (prepared by Kirill Borozdin)
problem Source:Ural FU Dandelion Contest. Petrozavodsk training camp. Summer 2014
There are two situations in which each character is inserted, creating a new palindrome tree node without generating a new node. So the answer will only be 0,1.
#include <iostream> #include <string.h> #include <stdlib.h> #include <math.h> #include < Stdio.h>using namespace Std;typedef long long int ll;const int max=5*1e6;const int Maxn=4*1e6+5;char str[max+5];struct tree{int next[maxn][2]; int fail[max+5]; int len[max+5]; int s[max+5]; int last,n,p; int new_node (int x) {memset (next[p],0,sizeof (next[p])); Len[p]=x; return p++; } void Init () {p=0; New_node (0); New_node (-1); last=0;n=0; S[0]=-1; Fail[0]=1;} int get_fail (int x) {while (S[n-len[x]-1]!=s[n]) x=fail[x]; return x; } int Add (int x) {x-= ' a '; S[++n]=x; int Cur=get_fail (last); if (! ( Last=next[cur][x]) {int now=new_node (len[cur]+2); Fail[now]=next[get_fail (Fail[cur])][x]; Next[cur][x]=now; Last=now; return 1; } return 0; }}tRee;char Ans[max+5];int Main () {while (scanf ("%s", str)!=eof) {tree.init (); int i; for (i=0;str[i];i++) {if (!tree.add (Str[i])) ans[i]= ' 0 '; elseans[i]= ' 1 ';} Ans[i]= ';p uts (ANS); return 0;}
URAL 2040 palindromes and Super abilities 2 (palindrome tree)