P3649 [APIO2014]回文串(回文自动机)

2021-02-03 06:18

阅读:367

标签:new   fine   queue   namespace   ||   回文   putchar   end   sch   

回文自动机裸题,把PAM建出来以后对每个节点更新答案即可

代码:

#include #define int long long
#define sc(a) scanf("%lld",&a)
#define scc(a,b) scanf("%lld %lld",&a,&b)
#define sccc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define schar(a) scanf("%c",&a)
#define pr(a) printf("%lld",a)
#define fo(i,a,b) for(int i=a;i#define re(i,a,b) for(int i=a;i#define rfo(i,a,b) for(int i=a;i>b;--i)
#define rre(i,a,b) for(int i=a;i>=b;--i)
#define prn() printf("\n")
#define prs() printf(" ")
#define mkp make_pair
#define pii pair#define pub(a) push_back(a)
#define pob() pop_back()
#define puf(a) push_front(a)
#define pof() pop_front()
#define fst first
#define snd second
#define frt front()
#define bak back()
#define mem0(a) memset(a,0,sizeof(a))
#define memmx(a) memset(a,0x3f3f,sizeof(a))
#define memmn(a) memset(a,-0x3f3f,sizeof(a))
#define debug
#define db double
#define yyes cout#define nno coutusing namespace std;
typedef vectorint> vei;
typedef vector vep;
typedef mapint,int> mpii;
typedef mapchar,int> mpci;
typedef mapstring,int> mpsi;
typedef dequeint> deqi;
typedef dequechar> deqc;
typedef priority_queueint> mxpq;
typedef priority_queueint,vectorint>,greaterint> > mnpq;
typedef priority_queue mxpqii;
typedef priority_queue,greater > mnpqii;
const int maxn=500005;
const int inf=0x3f3f3f3f3f3f3f3f;
const int MOD=100000007;
const db eps=1e-10;
int qpow(int a,int b){int tmp=a%MOD,ans=1;while(b){if(b&1){ans*=tmp,ans%=MOD;}tmp*=tmp,tmp%=MOD,b>>=1;}return ans;}
int lowbit(int x){return x&-x;}
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return aa:b;}
int mmax(int a,int b,int c){return max(a,max(b,c));}
int mmin(int a,int b,int c){return min(a,min(b,c));}
void mod(int &a){a+=MOD;a%=MOD;}
bool chk(int now){}
int half(int l,int r){while(lint m=(l+r)/2;if(chk(m))r=m-1;else l=m+1;}return l;}
int ll(int p){return p1;}
int rr(int p){return p1|1;}
int mm(int l,int r){return (l+r)/2;}
int lg(int x){if(x==0) return 1;return (int)log2(x)+1;}
bool smleql(db a,db b){if(areturn true;return false;}
db len(db a,db b,db c,db d){return sqrt((a-c)*(a-c)+(b-d)*(b-d));}
bool isp(int x){if(x==1)return false;if(x==2)return true;for(int i=2;i*iif(x%i==0)return false;return true;}
inline int read(){
    char ch=getchar();int s=0,w=1;
    while(ch48||ch>57){if(ch==-)w=-1;ch=getchar();}
    while(ch>=48&&ch57){s=(s1)+(s3)+ch-48;ch=getchar();}
    return s*w;
}
inline void write(int x){
    if(x0)putchar(-),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+48);
}

int ans=-inf;

struct PAM{
    int tot,last,n,cur;
    int fail[maxn],len[maxn],cnt[maxn],son[maxn][26];
    char s[maxn];
    int get(int p,int x){
        while(s[x-len[p]-1]!=s[x]) p=fail[p];
        return p;
    }
    int newnode(int x){
        len[tot]=x;
        return tot++;
    }
    void build(){
        scanf("%s",s+1);
        s[0]=-1,fail[0]=1,last=0;
        newnode(0),newnode(-1);
        for(n=1;s[n];++n){
            s[n]-=a;
            cur=get(last,n);
            if(!son[cur][s[n]]){
                int now=newnode(len[cur]+2);
                fail[now]=son[get(fail[cur],n)][s[n]];
                son[cur][s[n]]=now;
            }
            cnt[last=son[cur][s[n]]]++;
        }
        for(int i=tot-1;i>=0;--i)
            cnt[fail[i]]+=cnt[i];
        for(int i=tot-1;i>=0;--i)
            ans=max(ans,len[i]*cnt[i]);//
    }
}pam;

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    pam.build();
    pr(ans);
    return 0;
}

 

P3649 [APIO2014]回文串(回文自动机)

标签:new   fine   queue   namespace   ||   回文   putchar   end   sch   

原文地址:https://www.cnblogs.com/oneman233/p/11517332.html


评论


亲,登录后才可以留言!