当前位置:网站首页>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;
}
边栏推荐
- L2-026 小字辈 (25 分)
- 2019.10.30学习总结
- 2019.11.20训练总结
- 1147 Heaps (30 分)
- Caused by: org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
- Judgment of points inside and outside polygon
- Causes and solutions of error reporting by using startactivity() method outside the activity
- 在Activity外使用startActivity()方法报错原因与解决办法
- Codeforces Round #641 Div2
- Recyclerview refreshes blinks and crashes when deleting items
猜你喜欢
随机推荐
Codeforces Round #645 (Div. 2)
Signal works: time varying and time invariant
语言特性
Caused by: org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
基辅周边的凄美废墟——切尔诺贝利的安全前往指南!
FreeRTOS (IX) - queue
L2-031 深入虎穴 (25 分)
分布式和集群分不清,我们讲讲两个厨子炒菜的故事
Flutter 基础组件之 Image
Leetcode MySQL database topic 176
Gmail: how to quickly read all messages
1147 Heaps (30 分)
If I were in Beijing, where would it be better to open an account? In addition, is it safe to open an account online now?
setInterval、setTimeout和requestAnimationFrame
Dynamic linking of virtual machine stack of JVM
Is flush stock trading software reliable and safe?
2019.11.17训练总结
Pipeline details of IPC (interprocess communication)
C语言实现一种创建易管理易维护线程的方法
自定义控件之下载控件1(DownloadView1)








