URAL 2040 palindromes and Super abilities 2

Source: Internet
Author: User

Palindromes and Super abilities 2
Time Limit:500msmemory limit:102400kb64bit IO Format:%i64d &%i64u

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 InputAbbbba Sample Output111111NotesWe 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 solving: palindromictree
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =5000010;4 structpalindromictree{5     structnode{6         intson[2],f,len;7         voidInitintLen) {8Memset (son,0,sizeofson);9              This->len =Len;Ten         } One }E[MAXN]; A     intTot,last,n; -     CharS[MAXN]; -     intNewNodeintLen =0){ the E[tot].init (len); -         returntot++; -     } -     intGetfail (intx) { +          while(S[n-e[x].len-1] = S[n]) x =e[x].f; -         returnx; +     } A     voidinit () { atlast = Tot = n =0; -NewNode0); -NewNode (-1); -e[0].F =1; -S[n] =-1; -     } in     BOOLExtendintc) { -S[++n] =C; to         intCur =Getfail (last); +         BOOLIsextend = e[cur].son[c] = =0; -         if(!E[cur].son[c]) { the             intNewson = NewNode (E[cur].len +2); *E[NEWSON].F =E[getfail (E[CUR].F)].son[c]; $E[CUR].SON[C] =Newson;Panax Notoginseng         } -Last =E[cur].son[c]; the         returnIsextend; +     } A }pt; the CharSTR[MAXN]; + intMain () { -      while(gets (str)) { $ pt.init (); $          for(inti =0; Str[i]; ++i) { -             if(Pt.extend (Str[i)-'a')) Putchar ('1'); -             ElsePutchar ('0'); the         } -Putchar ('\ n');Wuyi     } the     return 0; -}
View Code

URAL 2040 palindromes and Super abilities 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.