BZOJ 3672([Apio2014]迴文串-迴文樹)

來源:互聯網
上載者:User

考慮一個只包含小寫拉丁字母的字串s。我們定義s的一個子串t的“出
現值”為t在s中的出現次數乘以t的長度。請你求出s的所有迴文子串中的最
大出現值。 字串長度≤300000

迴文樹模板題

建立迴文樹,則每個節點對應了一個本質不同的迴文串,出現次數為 cnti cnt_i
於是暴力遍曆即可

一個長度為 n n的字串本質不同的迴文串最多有 n n個。

#include<bits/stdc++.h>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=Pre[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define pb push_back#define mp make_pair #define MAXN (600000+10)typedef long long ll;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}namespace Palindromic_Tree {    int s[MAXN],n;    int tot,next[MAXN][26],link[MAXN],len[MAXN],last;    int cnt[MAXN];    int newnode(int l)    {        len[tot]=l;        return tot++;    }    void mem() {        MEM(s) MEM(next) MEM(link) MEM(len) MEM(cnt)        n=tot=0;        newnode(0); newnode(-1);        link[0]=link[1]=1; s[0]=27;        last=0;    }    int getnode(int x)    {        while (s[ n - len[x]-1 ]  != s[n] ) x=link[x];        return x;     }    void add(int c) {        s[++n]=c;//      cout<<"1";        int cur=getnode(last);        if (!next[cur][c])        {            int now=newnode(len[cur]+2);            int tmp=getnode(link[cur]);            link[now]=next[tmp][c];            next[cur][c] = now;        }               last=next[cur][c];        cnt[last]++;    }    void work()    {        RepD(i,tot) cnt[link[i]]+=cnt[i];        ll ans=0;        Fork(i,2,tot) {            ans=max(ans,1LL*cnt[i]*len[i]);        }         cout<<ans<<endl;    }}using namespace Palindromic_Tree;char S[MAXN];int N;int main(){//  freopen("bzoj3676.in","r",stdin);//  freopen(".out","w",stdout);    Palindromic_Tree::mem();    scanf("%s",S);    int N=strlen(S);    Rep(i,N) Palindromic_Tree::add(S[i]-'a');    Palindromic_Tree::work();     return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.