您的位置:首页 > 其它

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

2017-07-24 20:03 288 查看
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
;
int nex
;
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
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: