题目链接: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. Unittest 类方法

    import unittest,time from selenium import webdriver class TestClass(unittest.TestCase): @classmethod ...

  2. 阿里云 API 签名机制的 Python 实现

    在调用阿里云 API 的时候,最让人头疼的就是 API 的签名(Signature)机制,阿里云在通用文档中也有专项说明,但是仅仅有基于 Java 的实现代码示例.所以这里基于 Python 来分析下 ...

  3. 适合 ASP.NET Core 的超级-DRY开发

    作者 Thomas Hansen DRY 是那些非常重要的软件体系结构缩写之一.它的意思是“不要自我重复”,并向维护旧源代码项目的任何用户阐明了一个重要原则.也就是说,如果你在代码中自我重复,会发现每 ...

  4. Metasploaitable和侦察httrack-安全牛课堂网络安全之Web渗透测试练习记录

    环境配置 首先在网上下载kali的镜像以及Metasploaitable虚拟机,打开按照网上教程安装好kali虚拟机,另一边打开Metasploaitable虚拟机,进入输入初始账户msfadmin, ...

  5. 【剑指offer】构建乘积数组

    题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不 ...

  6. package.json中dependencies 与devDependencies 的区别

    首先,dependencies中安装的依赖是生产环境的依赖,即项目要运行所必须安装的包:devDependencies中安装的的依赖是开放环境的依赖,即在开发项目时需要安装的依赖. 其次,在安装依赖的 ...

  7. [转帖]浅谈P2P、P2C 、O2O 、B2C、B2B、 C2C的区别

    浅谈P2P.P2C .O2O .B2C.B2B. C2C的区别 https://www.cnblogs.com/zhuiluoyu/p/5481635.html 相信有很多人对P2P.P2C .O2O ...

  8. Qt3D NodeInstantiator 使用时报出index out of range错误的记录

    最近用到NodeInstantiator批量加入实体 刚开始用的时候一直程序崩溃 错误代码大致如下: // main.qml ApplicationWindow { ...... Loader { i ...

  9. FPGA成神之路

    先占个坑,网上写的都太没有体系了,打算写一个从电路到语法,从软件使用到硬件调试,从IP核调用到时序分析的系列帖子,人就是太懒,想把自己这两年踩的坑分享一下,加油,特种兵

  10. 【题解】与查询 [51nod1406]

    [题解]与查询 [51nod1406] 传送门:与查询 \([51nod1406]\) [题目描述] 给出 \(n\) 个整数,对于 \(x \in [0,1000000]\),分别求出在这 \(n\ ...