CodeForces - 888C K-Dominant Character 思维
题意
找到在整个字符串中 所有以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;
}
CodeForces - 888C K-Dominant Character 思维相关推荐
- Dominant Character 思维,字符串,贪心
题意 : 给一abc字符串,要求找出长度最短的子串 smallestsubstringsmallest substringsmallestsubstring(本题的阴间之处,不是找最小的子串的长度), ...
- Codeforces 1077B Disturbed People(思维题)
Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...
- K-Dominant Character CodeForces - 888C 思维 构造
题意 让我们在这个字符串中找一定k长度的字串中都至少含有一个相同的字符 让求这个字串的最小长度 分析 也就是说当我们最终确定了 字串的最小长度后 每个以此长度的字串都至少含有一个相同字符 那么当我们找 ...
- CodeForces - 160D Edges in MST(思维+tarjan/树链剖分+线段树)
题目链接:点击查看 题目大意:给出一张 n 个点 m 条边组成的带权无向图,现在对于每条边来说,确定一下其分类: 一定是最小生成树上的边 可能是最小生成树上的边 一定不是最小生成树的边 题目分析:两种 ...
- CodeForces - 1494E A-Z Graph(构造+思维)
题目链接:https://vjudge.net/problem/CodeForces-1494E 题目大意:给出一个初始时只有 nnn 个点的有向带权图,需要执行 mmm 次操作,每次操作分为下列三种 ...
- CodeForces - 1504C Balance the Bits(思维+构造)
题目链接:https://vjudge.net/problem/CodeForces-1504C 题目大意:给出一个长度为 nnn 的 010101 串,现在要求构造出两个长度为 nnn 的合法括号序 ...
- CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)
ACM思维题训练集合 A new Berland businessman Vitaly is going to open a household appliances' store. All he's ...
- CF--思维练习--CodeForces - 219C Color Stripe (思维)
ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...
- 【CodeForces - 215C 】Crosses (思维,图形题)
题干: There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 fr ...
最新文章
- 工业相机之全局曝光与卷帘曝光
- GBDT指标重要性计算
- python 微信模块_Python使用itchat模块实现简单的微信控制电脑功能示例
- pmbook 知识领域 第六版_PMP项目管理10大知识领域脑图
- PC-计算机动行命令里的密密!系统管理程序!
- php8更新,PHP 8 中新特性以及重大调整
- 阿里云Kubernetes CSI实践—NAS动态存储卷使用
- extmail html文件丢失,extmail/extman登录界面无法显示的故障解决
- 使用hierarchyid查询分层数据
- Script to Setup NTP/DNS/Powerpolicy for new server
- 《Oracle数据库管理与维护实战》——1.2 Oracle各版本异同
- java remote debug parameters
- Nexus-NuGet私有仓库服务搭建(一)
- Python学习(5)——内置函数
- 拨打国际电话的国际字冠和国家代码
- sem_wait和sem_post
- MySQL权限系统(一).The MySQL Access Privilege System 概述
- 腾讯地图、高德地图去除logo方法
- 多款AE字幕条模板动画
- 四、字符编码:ascii、gbk、Unicode、utf-8