K-Dominant Character CodeForces - 888C
题目链接:https://vjudge.net/problem/CodeForces-888C
划一条线,使得不论怎么划线,都会出现一个特定的字符,那么这条线最短要多长。
用字符间隔考虑。
先判断哪些字符出现了,然后统计每个不同字符的出现次数,出现一次的和出现多次的分开判断。
出现一次的找到它的位置,取max(当前位置 - 字符串开始位置 + 1,字符串末位位置 - 当前位置 + 1),
然后遍历所有出现一次的字符,得出max的最小值,并记录,dis1
出现多次的找到相邻两个相同字符的间隔,取最大间隔的间隔k,再得出第一个出现的相同字符和开头的间隔+1,最后一个出现的相同字符和字符串末尾的间隔+1,和k比较取最大dis2
答案就是min(dis1,dis2)。
#include <cstring>
#include <iostream>
#include <cstdio> using namespace std; #define inf (1LL << 30) const int N = ;
char str[N];
int tmp[N];
int arr[N];
bool vis[]; int main(){ ios::sync_with_stdio(false);
cin.tie(); cin >> str;
int len = strlen(str) - ; for(int i = ; i <= len; i++){
arr[i] = str[i] - 'a' + ;
vis[arr[i]] = true; //哪些字符出现过
} int dis1 = inf;
int dis2 = inf;
int l = ;
for(int o = ; o < ; o++){ //'a'~'z'
if(!vis[o]) continue; l = ;
for(int i = ; i <= len; i++){
if(o == arr[i]) tmp[l++] = i;
}
--l;
//出现一次的
if(l == ){
int v = ;
v = max(v,max(tmp[l] - + ,len - tmp[] + ));
dis1 = min(dis1,v);
}
//出现多次的
else{
int v = ;
for(int i = ; i <= l; i++){
v = max(v,tmp[i] - tmp[i - ]);
} v = max(v,tmp[] - + ); //与字符串开头
v = max(v,len - tmp[l] + );//与字符串结尾 dis2 = min(dis2,v);
}
} cout << min(dis1,dis2) << endl; getchar();getchar(); return ;
}
K-Dominant Character CodeForces - 888C的更多相关文章
- Codeforces 888C: K-Dominant Character(水题)
You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff ...
- Codeforces Round #754 (Div. 2) C. Dominant Character
题目:Problem - C - Codeforces 如代码,一共有七种情况,注意不要漏掉 "accabba" , "abbacca" 两种情况: 使用 ...
- K Balanced Teams CodeForces - 1133E (Dp)
题意: 给出 n 个数,选取其中若干个数分别组成至多 k 组,要求每组内最大值与最小值的差值不超过5,求最后被选上的总人数. 题解: 将a[1∼n] 从小到大排序, f[i][j] 表示到第 i 个数 ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...
- 启发式合并CodeForces - 1009F
E - Dominant Indices CodeForces - 1009F You are given a rooted undirected tree consisting of nn vert ...
- Leetcode: Rearrange String k Distance Apart
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- Longest Substring with At Most K Distinct Characters
Given a string, find the longest substring that contains only two unique characters. For example, gi ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
随机推荐
- 根据 oracle 标准计算超长字符串的长度
Oracle 数据库使用 sql语句 : select lengthb('输入字符串') from dual , 来计算 字符串 所占的字节长度(比如,一个汉字3个字节),但是用这个leng ...
- 深度学习之ResNet网络
介绍 Resnet分类网络是当前应用最为广泛的CNN特征提取网络. 我们的一般印象当中,深度学习愈是深(复杂,参数多)愈是有着更强的表达能力.凭着这一基本准则CNN分类网络自Alexnet的7层发展到 ...
- 秘制牛肉Gamma阶段项目展示
秘制牛肉Gamma阶段项目展示 1.团队成员和个人博客 · 左顺:在项目中主要负责后端开发.个人博客 · 袁勤:精通网页项目开发,前端后端都很强,在完成自己后端任务后也会积极帮助其他人的任务.个人博客 ...
- ASP.NET之MVC 微信公众号授权给第三方平台的技术实现流程(获取第三方平台access_token)
“出于安全考虑,在第三方平台创建审核通过后,微信服务器每隔10分钟会向第三方的消息接收地址推送一次component_verify_ticket,用于获取第三方平台接口调用凭据”.这是文档中的原话,也 ...
- 修改Mac新建文件、文件夹的默认权限
Mac 新建文件和文件夹默认 当前用户才有读和写的权限,其他用户只有读的权限 修改Mac新建文件.文件夹的默认权限的步骤如下: 1.打开 “终端” 2.在终端中输入以下命令: 创建文件时,从默认值(对 ...
- @AspectJ注解的value属性
@Component @Scope("prototype") @Aspect(value="perthis(execution(* com.helius.service. ...
- Cannot find class in classpath解决方法
1)Build Path出问题了 build path出问题了 ,java工程名前会有一个红色的感叹号,重新build一下 工程名上右键——>Build Path ——>Configure ...
- [转帖]如何获得一个RAC Oracle数据库(从Github - oracle/docker-images) - 本地版 ---暂时未做实验.
如何获得一个RAC Oracle数据库(从Github - oracle/docker-images) - 本地版 2019-11-09 16:35:30 dingdingfish 阅读数 32更多 ...
- [转帖]Elasticsearch数据库
Elasticsearch数据库 2018-05-06 11:30:48 七色米 阅读数 23634更多 分类专栏: 数据库 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转 ...
- GO中标识符,标识符,变量,常量,iota
参考:https://www.cnblogs.com/nickchen121/p/11517455.html 一.标识符与关键字 1.标识符 在编程语言中标识符就是程序员定义的具有特殊意义的词,比如变 ...