当前位置:网站首页>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;
}
边栏推荐
- MySQL modify auto increment initial value
- 微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch
- Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail
- 2020-9-14 广告系统入门
- ORA-01950 对表空间无权限
- Custom MVC framework implementation
- 2019.10.30学习总结
- Memory layout of JVM objects
- leetcode MYSQL数据库题目181
- How to traverse objects in the vector container
猜你喜欢

Student addition / deletion gaih

Container of the basic component of the flutter

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI

Listview of the basic component of the shutter

A method of creating easy to manage and maintain thread by C language

FreeRTOS(九)——队列

力扣94二叉树的中序遍历

装饰器模式的应用,包装ServletRequest,增加addParameter方法

自定义控件之侧滑关闭 Activity 控件

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
随机推荐
Setinterval, setTimeout and requestanimationframe
Data governance: data standard management (Part III)
JS获取手机型号和系统版本
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
linux环境下安装配置redis,并设置开机自启动
内网穿透工具frp使用入门
Gmail:如何快速将邮件全部已读
在Activity外使用startActivity()方法报错原因与解决办法
自定义mvc框架实现
RecyclerView刷新闪烁与删除Item时崩溃问题
mysql修改自动递增初始值
leetcode MYSQL数据库题目178
MySQL modify auto increment initial value
gcc与makefile
IPC(进程间通信)之管道详解
leetcode MYSQL数据库题目180
The 23 most useful elasticsearch search techniques you must know
JVM method return address
Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail
微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch