题目链接: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的更多相关文章

  1. Codeforces 888C: K-Dominant Character(水题)

    You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff ...

  2. Codeforces Round #754 (Div. 2) C. Dominant Character

    题目:Problem - C - Codeforces 如代码,一共有七种情况,注意不要漏掉  "accabba"  , "abbacca"  两种情况: 使用 ...

  3. K Balanced Teams CodeForces - 1133E (Dp)

    题意: 给出 n 个数,选取其中若干个数分别组成至多 k 组,要求每组内最大值与最小值的差值不超过5,求最后被选上的总人数. 题解: 将a[1∼n] 从小到大排序, f[i][j] 表示到第 i 个数 ...

  4. 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 ...

  5. 启发式合并CodeForces - 1009F

    E - Dominant Indices CodeForces - 1009F You are given a rooted undirected tree consisting of nn vert ...

  6. 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 ...

  7. 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 ...

  8. Longest Substring with At Most K Distinct Characters

    Given a string, find the longest substring that contains only two unique characters. For example, gi ...

  9. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

随机推荐

  1. linux脚本中有source相关命令时的注意事项

    写这个问题起因是因为一个同学去的java一键脚本环境变量设置问题, [root@localhost u01]# more 1.sh #!/bin/bash grep -q "export J ...

  2. 牛逼哄哄的 Lambda 表达式,简洁优雅就是生产力!

    阅读本文大概需要 4 分钟. 作者:Sevenvidia https://www.zhihu.com/question/20125256/answer/324121308 什么是Lambda? 我们知 ...

  3. BeanPostProcessor

    BeanPostProcessor简介 BeanPostProcessor是Spring IOC容器给我们提供的一个扩展接口.接口声明如下: public interface BeanPostProc ...

  4. JNA 调用操作系统函数 和 系统调用

    linux系统调用syscall 表:https://filippo.io/linux-syscall-table/ Linux Namespace 特性简要介绍 原文:https://iliangq ...

  5. Java编程思想之十 内部类

    可以将一个类定义放在另一个类的定义内部,这就是内部类. 10.1 创建内部类 创建内部类就是把类的定义置于外部类里面. public class Parcell { class contents{ i ...

  6. Hadoop vs Elasticsearch – Which one is More Useful

    Hadoop vs Elasticsearch – Which one is More Useful     Difference Between Hadoop and Elasticsearch H ...

  7. kali 破解wifi

    1.查看网卡名称:iwconfig 有一块 名为wlan0 的无线网卡通过一下命令排除影响因素 root@jkali:~# iwconfig lo no wireless extensions. wl ...

  8. 在博文顶部添加文章字数及阅读时间信息:阅读本文需要xx分钟

    1.在博客园设置中,页首Html代码中添加js代码 2.js代码如下 <script type="text/javascript"> $("#cnblogs_ ...

  9. 在RedisTemplate中使用scan代替keys指令

    keys * 这个命令千万别在生产环境乱用.特别是数据庞大的情况下.因为Keys会引发Redis锁,并且增加Redis的CPU占用.很多公司的运维都是禁止了这个命令的 当需要扫描key,匹配出自己需要 ...

  10. PowerBuilder学习笔记之2PowerScript语言(一)

    教材链接:https://wenku.baidu.com/view/1e82d26925c52cc58ad6be05.html?sxts=1565679996440 2.1PowerScript基础 ...