CodeForces
来源:互联网 发布:王健林八字 知乎 编辑:程序博客网 时间:2024/06/05 13:53
题意
找到在整个字符串中 所有以k长度的子串中的公共串至少长度为1的字符串 求出这个K的最小值是多少
分析
每个子串都有的至少长度为1的公共子串 这里我们要求k的最小 那么不如就把公共串的长度考虑为1 因为如果存在一个公共串长度为k>1的串 那么他必然满足k=1时的解 所以这里不如简化问题
统计下每个字符的最长距离就可以了 求出所有出现过的字符的最长距离中的最小值 就是k长度子串的最小值
复杂度O(n)
#include<bits/stdc++.h>using namespace std;typedef long long ll;char a[100010];int pos[30],ans[30];// pos存放最长距离 ans中存放每个字符的最大距离 其中取最小的就是结果 const int inf = 900010;int main(){ gets(a); int c=0,len = strlen(a); memset(ans,-1,sizeof(ans)); for(int i=0;i<len;i++) { if(ans[a[i]-'a']==-1)ans[a[i]-'a'] = i+1;//注意前后要处理一下 每个字符应当视为和第一个 最后一个字符的距离也要算进去 ans[a[i]-'a']=max(i-pos[a[i]-'a'],ans[a[i]-'a']); pos[a[i]-'a']=i; } //找到最长距离 这个就是每两个相同字符之间的最长距离 //找到最长距离后 遍历每个字符找到最长中的最短就是解 for(int i=0;i<26;i++) if(ans[i]!=-1)ans[i] = max(len-pos[i],ans[i]); int res=inf; for(int i=0;i<26;i++){ if(ans[i]!=-1) res = min(ans[i],res); } printf("%d\n",res); return 0;}
阅读全文
0 0
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Caffe源码:math_functions 分析
- 51nod 1390 游戏得分
- cann't create instance in all devModuleCode.Please check register info in table pub_eventlistener
- PHP编译环境配置之EditPlus
- LSTM网络介绍
- CodeForces
- 使用ijkplayer播放一个网络视频
- QT中QWidget、QDialog及QMainWindow的区别
- Spring整合redis方法总结
- JVM原理讲解和调优
- shadowsocks配置记录
- 欢迎使用CSDN-markdown编辑器
- C++学习笔记 ---- 重载
- (原创)Android6.0亮屏流程之Keyguard Window绘制