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 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
随机推荐
- terminal使用kubectl.exe delete pod podname删不掉
今天通过kubernetes的dashboard进行删除有问题或者重启次数太多的pod,发现删不掉,然后就在本地尝试使用terminal进行删除 先获取指定namespace下的所有的pod,根据st ...
- IE浏览器卡死提示是否停止运行此脚本的解决办法
IE浏览器经常卡死,报是否停止运行此脚本,严重影响使用体验,下面小编教大家怎么解决这个问题,供大家参考! 1.启动IE浏览器,点击上方菜单栏位的工具,如下图所示 2.在工具栏位选择internet选项 ...
- java对象转变为map
直接上代码 package com.**.**.**.common; import com.**.**.**.util.JsonUtils; import org.springframework.be ...
- Shell脚本之六 数学计算
前面一节Shell篇之五 基本运算符介绍了常见的 Shell 算术运算符,这节介绍 Shell 的数学计算.Shell 和其它编程语言不同,Shell 不能直接进行算数运算,必须使用数学计算命令. 下 ...
- word 转 pfd
转自: https://www.cnblogs.com/qiwu1314/p/6101400.html demo: public class Doc2Pdf { public static boole ...
- vue日历/日程提醒/html5本地缓存
先上图 功能: 1.上拉日历折叠,展示周 2.左右滑动切换月 2.“今天”回到今天:“+”添加日程 3.localStorage存储日程 index,html <body> <div ...
- 【转载】socket通信-C#实现tcp收发字符串文本数据
在日常碰到的项目中,有些场景需要发送文本数据,也就是字符串,比如简单的聊天文字,JSON字符串等场景.那么如何如何使用SharpSocket来收发此类数据呢?其中要掌握的关键点是什么呢? 点击查看原博 ...
- 《C++ Primer》学习总结;兼论如何使用'书'这种帮助性资料
6.25~ 6.27,用了3天翻了一遍<C++ Primer>. ▶书的 固有坏处 一句话: 代码比 文字描述 好看多了.————> 直接看习题部分/ 看demo就行了 看文字在描述 ...
- 安装多个jdk导致eclipse打不开问题
问题描述 本来使用的是jdk1.8,由于其他原因需要使用jdk1.6,在安装完jdk1.6后打开eclipse就会报错: Version 1.6.0_43 of the JVM is not suit ...
- Java自学-类和对象 类方法
Java的类方法和对象方法 类方法: 又叫做静态方法 对象方法: 又叫实例方法,非静态方法 访问一个对象方法,必须建立在有一个对象的前提的基础上 访问类方法,不需要对象的存在,直接就访问 步骤 1 : ...