当前位置:网站首页>51nod1277 maximum value in string [KMP]
51nod1277 maximum value in string [KMP]
2022-06-29 10:11:00 【Yekaterina 2】
The prefix of a string is a continuous substring containing the first letter of the character , for example :abcd All prefixes of are a, ab, abc, abcd. Give a string S, Find all the prefixes , The maximum value of the product of the character length and the number of occurrences .
for example :S = “abababa” All prefixes are as follows :
“a”, The product of length and number of occurrences 1 * 4 = 4,
“ab”, The product of length and number of occurrences 2 * 3 = 6,
“aba”, The product of length and number of occurrences 3 * 3 = 9,
“abab”, The product of length and number of occurrences 4 * 2 = 8,
“ababa”, The product of length and number of occurrences 5 * 2 = 10,
“ababab”, The product of length and number of occurrences 6 * 1 = 6,
“abababa”, The product of length and number of occurrences 7 * 1 = 7.
among "ababa" There is 2 Time , The product of the two is 10, Is the largest of all prefixes .
Forward thinking :
By seeking next Array and recursive next The array method records the number of occurrences of the string .
Code :
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#define N 100005
using namespace std;
typedef long long ll;
int f[N],cnt[N];
char str[N];
int len;
void getnext(){
f[0]=0,f[1]=0;
for(int i=1;i<len;i++){
int j=f[i];
while(j&&str[i]!=str[j])j=f[j];
f[i+1]=(str[i]==str[j]?j+1:0);
}
}
int main(){
cin>>str;
len=strlen(str);
getnext();
for(int i=len;i>=1;i--){
cnt[i]++;
cnt[f[i]]+=cnt[i];
}
ll res=0;
for(ll i=1;i<=len;i++){
res= max(i*cnt[i],res);
}
cout<<res<<endl;
}
边栏推荐
猜你喜欢

Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks

Binding mechanism of JVM methods

JVM之方法的绑定机制

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit

JVM之TLAB

nacos注册中心集群

任务调度器之Azkaban的使用

Using rancher to build kubernetes cluster

阿里云防火墙配置,多种设置方式(iptables和fireward)

Image of the basic component of the shutter
随机推荐
Judgment of points inside and outside polygon
图片验证码控件
Codeforces Round #659 (Div. 2)
L1-009 N个数求和 (20 分)
LiferayPortal JSONWS反序列化漏洞(CVE-2020-7961)分析
GCC and makefile
Causes and solutions of error reporting by using startactivity() method outside the activity
1147 Heaps (30 分)
信号作品:时变和时不变
RecyclerView刷新闪烁与删除Item时崩溃问题
Force deduction 85 question maximum rectangle
力扣85题最大矩形
Gmail:如何快速将邮件全部已读
2019.10.6训练总结
蛇形填数
ImageView picture fill problem
分布式和集群分不清,我们讲讲两个厨子炒菜的故事
Using rancher to build kubernetes cluster
2019.10.27训练总结
Leetcode MySQL database topic 177