CF 888C K-Dominant Character(模拟)

题目链接:http://codeforces.com/problemset/problem/888/C

题目:

You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least k contains this character c.

You have to find minimum k such that there exists at least one k-dominant character.

Input

The first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).

Output

Print one number — the minimum value of k such that there exists at least one k-dominant character.

Examples

input

abacaba

output

2

input

zzzzz

output

1

input

abcde

output

3

题意:字符串任意k长度子串都要包含某个字母,问长度k最小为多少。

题解:先预处理把相同字母之间的长度都存到一个数组里(这里注意最前面和最后面的处理),都要包含某个字母,那么我们肯定要取数组里面的最大值,然后把最大值比较一下,取下最小值就OK啦。

 1 #include <vector>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 vector <int> T[30];
 7 vector <int> res[30];
 8 const int INF=0x3f3f3f3f;
 9
10 int main(){
11     int len,tmp,t,ans=INF;
12     string s;
13     cin>>s;
14     len=s.size();
15     for(int i=0;i<26;i++) T[i].push_back(-1);//最前面的处理
16     for(int i=0;i<len;i++){
17         tmp=s[i]-‘a‘;
18         t=i-T[tmp].back();
19         T[tmp].push_back(i);
20         res[tmp].push_back(t);
21     }
22     for(int i=0;i<26;i++){
23         t=len-T[i].back();//最后面的处理
24         res[i].push_back(t);
25         sort(res[i].begin(),res[i].end());
26     }
27     for(int i=0;i<26;i++){
28         if(res[i].size()>1){
29             ans=min(ans,res[i].back());
30         }
31     }
32     if(ans==INF) cout<<len/2+1<<endl;
33     else cout<<ans<<endl;
34     return 0;
35 }
时间: 2024-07-30 06:09:16

CF 888C K-Dominant Character(模拟)的相关文章

CF 378(2) C D 模拟

CF 378(2)   好坑,有时间再做一遍 CodeForces 733C 题意:n只怪物,每只重ai,一开始有给定序列a[].问最后是否能变到x只特定序列b[],变化只能是相邻的大吃小. 题解:坑死人的题,,但不要怕去写这种题,在草稿纸上写好思路,一定要动手写.  思路:先把a[]根据b[]进行分段,再对每一段进行分析.细节太多,不自己动手写一遍根本体会不到.. #include<bits/stdc++.h> using namespace std; #pragma comment(lin

CF #392(2) C 暴力模拟

CF #392(2)  C. Unfair Poll 题意:n行m列人,老师点k次名.点名次序,每一行都是从1到m,但行是按1,2....(n-1),n,(n-1),(n-2)...1,2,3....(n-1),n.....求点完k次名后被点的最多的次数和最少的次数,以及给定的(x,y)被点次数. 总结:有点麻烦,但还是很好找规律,只是fst了,有时间再写一遍...还是太菜了,连着三场CF都是fst #include<bits/stdc++.h> using namespace std; #p

HDU 5122 K.Bro Sorting(模拟——思维题详解)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will compare each pair of adjacent items and swap them if they are in the wrong o

CF 2A Winner(STL+模拟)

题目链接:http://codeforces.com/contest/2/problem/A 题目: The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points,

[Tyvj1001]第K极值 (贪心?模拟)

考前打tyvj的水题 题目描述 给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字a[i]是小于maxlongint的非负整数 ,编程要求求出整个序列中第k大的数字减去第k小的数字的值m,并判断m是否为质数.(0<k<=n) 输入格式 输入格式:第一行为2个数n,k(含义如上题)第二行为n个数,表示这个序列 输出格式 输出格式:如果m为质数则第一行为'YES'(没有引号)第二行为这个数m否则 第一行为'NO'第二行为这个数m 测试数据 5 2 1 2 3 4 5

hdu4280 Island Transport(最大流Dinic数组模拟邻接连边)

Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 10716    Accepted Submission(s): 3430 Problem Description In the vast waters far far away, there are many islands. People are l

Longest Substring with At Most K Distinct Characters

Given a string, find the longest substring that contains only two unique characters. For example, given "abcbbbbcccbdddadacb", the longest substring that contains k unique character is "bcbbbbcccb". 分析: 用hashmap记录每个character从start到当前位置

cf 434d Nanami&#39;s Power Plant 网络流

题目大意就是有n个发电机,每个发电机有l到r个档位可供选择,每个档位的输出功率是已知的,另外还有一些限制条件,形式为xu ≤ xv + d,表示发电机u的档位要小于v的档位加d,d是一个已知的整数.求n个发电机的最大功率. 假设没有最后那个限制条件,那么对于每个发电机i拆点成l-1,l...到r相邻两档位连边为max-f(i),f(i)是档位i的输出功率,max是一个大数,大于等于所有档位的输出功率.l-1与源点连inf的边,r与汇点连inf的边.假设最大流为flow,那么max*n-flow就

模拟专题训练小结

本周我进行了针对模拟题的训练,在比赛中,模拟题是考察一个人代码综合能力的最佳体现,也是考验选手对细节的把握能力的有力工具.本周的模拟题大多来自WF和Regional,而前者的模拟题一般细节比较多,编程比较复杂.下面来根据我本周的训练情况来简要总结一下这方面需要注意的地方. 首先,应该完全理解题意,这一步相当关键,如果连题意都理解错了,那么后面无论花多大功夫去调试代码都是无济于事,因为你一直在实现一个错误的模拟过程.弄明白题目的全部流程,接下来就是考虑顶层设计:有哪些全局变量,需要设计哪些结构体,