当前位置:网站首页>51nod1277 字符串中的最大值【KMP】
51nod1277 字符串中的最大值【KMP】
2022-06-29 09:17:00 【叶卡捷琳娜2号】
一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd。给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值。
例如:S = “abababa” 所有的前缀如下:
“a”, 长度与出现次数的乘积 1 * 4 = 4,
“ab”,长度与出现次数的乘积 2 * 3 = 6,
“aba”, 长度与出现次数的乘积 3 * 3 = 9,
“abab”, 长度与出现次数的乘积 4 * 2 = 8,
“ababa”, 长度与出现次数的乘积 5 * 2 = 10,
“ababab”, 长度与出现次数的乘积 6 * 1 = 6,
“abababa”, 长度与出现次数的乘积 7 * 1 = 7.
其中"ababa"出现了2次,二者的乘积为10,是所有前缀中最大的。
正解思路:
通过求next数组 和 递归next数组的方法记录字符串出现的次数。
代码:
#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;
}
边栏推荐
- 我想要股票开户优惠,怎么得到?还有,在线开户安全么?
- 2019.11.3学习总结
- 監控數據源連接池使用情况
- Is it safe to open an account for stock speculation? Is it reliable?
- Student增删gaih
- Hystrix熔断器:服务熔断与服务降级
- leetcode MYSQL数据库题目178
- The collapsing "2.3 * 10 = 22" produced by multiplying float and int
- Image of the basic component of the shutter
- Application of decorator mode, packaging ServletRequest and adding addparameter method
猜你喜欢

Alternative implementation of Scrollview pull-down header amplification

JVM之对象的内存布局

JVM method return address

JVM之TLAB

Binding mechanism of JVM methods

Cisco ASA、FTD和HyperFlex HX的漏洞分析复现

Dynamic linking of virtual machine stack of JVM

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

ORA-01950 对表空间无权限

弧形 View 和弧形 ViewPager
随机推荐
券商经理给的开户二维码办理股票开户安全吗?我想开个户
2020-09-29 非商品模板化代码层次 rapidjson库
Cloud management platform: openstack architecture design and detailed interpretation
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
FreeRTOS (IX) - queue
弧形 View 和弧形 ViewPager
Minorgc, majorgc, fullgc
Mysql5.7 installation tutorial in centos7 under Linux
In XML layout, the button is always displayed on the top layer
我想知道如何免费网上注册股票开户?另外,手机开户安全么?
Please use the learned knowledge to write a program to find out the password hidden in the long string below. The burial point of the password conforms to the following rules:
Leetcode MySQL database topic 177
FreeRTOS(九)——队列
GridView of basic component of shutter
Data governance: data standard management (Part III)
Surveiller l'utilisation du pool de connexion des sources de données
Generic paging framework
A method of creating easy to manage and maintain thread by C language
Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
2020-09-18 referer认证 url转义