当前位置:网站首页>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;
}
边栏推荐
- [Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)
- A 2.5D Cancer Segmentation for MRI Images Based on U-Net
- 详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
- 弧形 View 和弧形 ViewPager
- 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:
- Setinterval, setTimeout and requestanimationframe
- LiferayPortal JSONWS反序列化漏洞(CVE-2020-7961)分析
- C语言实现一种创建易管理易维护线程的方法
- Data governance: data standard management (Part III)
- Application of decorator mode, packaging ServletRequest and adding addparameter method
猜你喜欢
随机推荐
linux下centos7中mysql5.7安装教程
2019.11.17训练总结
在Activity外使用startActivity()方法报错原因与解决办法
Flutter 基础组件之 Container
Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
ImageView图片填充问题
leetcode MYSQL数据库题目178
linux环境下安装配置redis,并设置开机自启动
How to traverse objects in the vector container
Generic paging framework
Constructing SQL statements by sprintf() function in C language
In XML layout, the button is always displayed on the top layer
TLAB of JVM
Student addition / deletion gaih
gcc与makefile
微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch
Binding mechanism of JVM methods
cenos7下搭建LAMP环境
阿里云防火墙配置,多种设置方式(iptables和fireward)
Community Union









