Bzoj 3676 [Apio2014] palindrome string (palindrome tree)

Source: Internet
Author: User

"Topic link" http://www.lydsy.com/JudgeOnline/problem.php?id=3676

"The main topic"

Consider a string s that contains only the lowercase Latin alphabet.
We define the "occurrence value" of a substring t of S as the number of occurrences of T in s multiplied by the length of T.
The maximum occurrence value in all palindrome substrings of S.

Exercises

We give a string to establish a palindrome tree, statistics of the occurrence of each palindrome string and length, multiply to take the group large can

Code

#include <cstdio> #include <algorithm> #include <cstring> using namespace std;const int n=300010,s=26;    int all,son[n][s],fail[n],cnt[n],len[n],text[n],last,tot;int newnode (int l) {for (int i=0;i<s;i++) son[tot][i]=0;    Cnt[tot]=0,len[tot]=l; return tot++;}    void Init () {last=tot=all=0;    NewNode (0), NewNode (-1); Text[0]=-1,fail[0]=1;}    int getfail (int x) {while (Text[all-len[x]-1]!=text[all]) x=fail[x]; return x;}    void Add (int w) {text[++all]=w;    int X=getfail (last);        if (!son[x][w]) {int y=newnode (len[x]+2);        Fail[y]=son[getfail (Fail[x])][w];    Son[x][w]=y; }cnt[last=son[x][w]]++;} void Count () {for (int i=tot-1;~i;i--) cnt[fail[i]]+=cnt[i];}         Char S[n];int main () {while (~SCANF ("%s", s)) {int N=strlen (s);        Init ();        for (int i=0;i<n;i++) Add (s[i]-' a '); Count ();        Long Long ans=0;        for (int i=0;i<tot;i++) Ans=max (Ans,1ll*cnt[i]*len[i]);    printf ("%lld\n", ans); }return 0;}

Bzoj 3676 [Apio2014] palindrome string (palindrome tree)

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.