gggyt  
没谁离不开谁

  模板题。问你一个串里最大的值(回文子串*出现次数)

 /* gyt

       Live up to every day            */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1e6+10;
const int sigma=26;
const ll mod = 1000000007;
const int INF = 0x3f3f3f;
const db eps = 1e-9;
struct ptree{
    char s[maxn];
    int next[maxn][sigma], fail[maxn], cnt[maxn], len[maxn];
    int last, n, p;
    ll res;
    inline int newnode(int l) {
        cnt[p]=0;
        len[p]=l;
        return p++;
    }
    inline void init() {
        n=0, p=0, last=0;

      memset(next, 0, sizeof(next));
      memset(cnt, 0, sizeof(cnt));
      memset(len, 0, sizeof(len));
      memset(fail, 0, sizeof(fail));

        newnode(0), newnode(-1);
        s[n]=-1;
        fail[0]=1;
        //cout<<n<<" "<<p<<" "<<last<<endl;
    }
    inline int FL(int x) {
        while(s[n-len[x]-1]!=s[n])  x=fail[x];
        return x;
    }
    void add(char c) {
        c-='a';
        s[++n]=c;
        int cur=FL(last);
        if (!next[cur][c]) {
            int now=newnode(len[cur]+2);
            fail[now]=next[FL(fail[cur])][c];
            next[cur][c]=now;
        }
        last=next[cur][c];
        ++cnt[last];
    }
    inline ll countt() {
        ll pk=0;
        for (int i=p-1; ~i; --i) {
            cnt[fail[i]]+=cnt[i];
            pk=max(pk, (ll)cnt[i]*(ll)len[i]);
        }
        return pk;
    }
}p;
char s[maxn];
void solve(){
    scanf("%s",s);
    int len=strlen(s);
    p.init();
    for(int i=0;i<len;i++)
        p.add(s[i]);
    printf("%lld\n",p.countt());
}
int main() {
    int t = 1;
   // freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //scanf("%d", &t);
    while(t--)
        solve();
    return 0;
}

 

posted on 2017-10-05 14:26  gggyt  阅读(164)  评论(0编辑  收藏  举报