Ural 1960 Palindromes and Super abilities

Source: Internet
Author: User

Palindromes and Super abilitiestime limit:1000msmemory limit:65536kbthis problem would be judged onUral. Original id:1960
64-bit integer IO format: %lld Java class name: (any) After solving seven problems on Timus Online Judge with a word ' palindrome ' in the problem name, Misha have got an unusual Ability. Now if he reads a word, he can mentally count the number of unique nonempty substrings of this word is palindrome S.dima wants to test Misha ' s new ability. He adds letters s1,..., s NTo a word, letter by letter, and after every letter asks Misha, how many different nonempty palindromes current word conta Ins as substrings. which NNumbers would Misha say, if he'll never be wrong?InputThe only line of input contains the string s1 ... s N, where s Iis small 中文版 letters (1≤ N≤105).OutputOutput NNumbers separated by whitespaces, I-th of these numbers must be the number of different nonempty substrings of prefix s1 ... s IThat is palindromes.Sample Input
Aba
Sample Output
1 2 3
SourceUral FU Contest. Kontur Cup. Petrozavodsk training camp. Winter 2013 Problem Solving: palindromictree
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =100010;4 structpalindromictree{5     intfail[maxn],len[maxn],son[maxn][ -];6     intTot,last,n;7     CharS[MAXN];8     intNewNodeintSlen =0){9memset (Son[tot],0,sizeofSon[tot]);TenLen[tot] =Slen; One         returntot++; A     } -     voidinit () { -n = tot = last =0; theNewNode0); -NewNode (-1); -fail[1] = fail[0] =1; -S[n] =-1; +     } -     intGetfail (intx) { +          while(S[n-len[x]-1] = S[n]) x =Fail[x]; A         returnx; at     } -     voidExtendintc) { -S[++n] =C; -         intCur =Getfail (last); -         if(!Son[cur][c]) { -             intx = NewNode (Len[cur] +2); inFAIL[X] =Son[getfail (Fail[cur])][c]; -SON[CUR][C] =x; to         } +Last =Son[cur][c]; -     } the }pt; * CharSTR[MAXN]; $ intMain () {Panax Notoginseng      while(~SCANF ("%s", str)) { - pt.init (); the         BOOLFlag =false; +          for(inti =0; Str[i]; ++i) { APt.extend (Str[i]-'a'); the             if(flag) Putchar (' '); +printf"%d", Pt.tot-2); -Flag =true; $         } $Putchar ('\ n'); -     } -     return 0; the}
View Code

Ural 1960 Palindromes and Super abilities

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.