【Kmp求字符串前缀在字符串出现的次数】51nod 1277 字符串中的最大值

来源:互联网 发布:linux服务器换编码 编辑:程序博客网 时间:2024/05/16 15:46

Link:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1277

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;/*51nod 1277 字符串中的最大值求前缀在字符串出现的次数 (num[i])num[nex[i]] += num[i];*/const int N = 100010;char s[N];int nex[N];void getnext(){    int len = strlen(s);    int k = -1;    nex[0] = -1;    for(int i = 1; i < len; i++){        while(k!=-1 && s[k+1]!=s[i])            k = nex[k];        if(s[k+1]==s[i])            k++;        nex[i] = k;    }}int mp[N];int main(){    scanf("%s",s);    getnext();    int len = strlen(s);    memset(mp,0,sizeof(mp));    for(int i = len; i >= 1; i--){        mp[i]++;        mp[nex[i-1]+1] += mp[i];//        printf("%d %d \n",i,mp[i]);    }    LL mx = 0;    for(int i = 1; i <= len; i++){        mx = max(mx,(LL)i*(LL)mp[i]);    }    printf("%lld\n",mx);    return 0;}