zoukankan      html  css  js  c++  java
  • 洛谷P3649 [APIO2014]回文串(回文自动机)

    传送门

    话说回文自动机我自己都还没搞懂呢……

    等到时候会了再来填坑

     1 //minamoto
     2 #include<cstdio>
     3 #include<cstring>
     4 #define ll long long
     5 template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
     6 const int N=3e5+5;
     7 char s[N];
     8 int n,p,q,fail[N],cnt[N],len[N],tot,last,ch[N][26];
     9 ll ans;
    10 inline int newnode(int x){
    11     len[++tot]=x;return tot;
    12 }
    13 inline int getfail(int x,int n){
    14     while(s[n-len[x]-1]!=s[n]) x=fail[x];
    15     return x;
    16 }
    17 int main(){
    18     scanf("%s",s+1);
    19     s[0]=-1,fail[0]=1,last=0;
    20     len[0]=0,len[1]=-1,tot=2;
    21     for(int i=1;s[i];++i){
    22         s[i]-='a';
    23         p=getfail(last,i);
    24         if(!ch[p][s[i]]){
    25             q=newnode(len[p]+2);
    26             fail[q]=ch[getfail(fail[p],i)][s[i]];
    27             ch[p][s[i]]=q;
    28         }
    29         ++cnt[last=ch[p][s[i]]];
    30     }
    31     for(int i=tot;i;--i)
    32     cnt[fail[i]]+=cnt[i],cmax(ans,1ll*cnt[i]*len[i]);
    33     printf("%lld
    ",ans);
    34     return 0;
    35 }
  • 相关阅读:
    SQL语法分类
    SQL语法入门
    数据库的基本对象
    数据库基础
    数据库概述
    设计模式之备忘录模式
    设计模式之State模式
    设计模式之装饰模式
    简单工厂模式
    初识C#设计模式
  • 原文地址:https://www.cnblogs.com/bztMinamoto/p/9629587.html
Copyright © 2011-2022 走看看