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

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;
}


    原文作者:KMP算法
    原文地址: https://blog.csdn.net/qq_33199236/article/details/76037445
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞