Educational Codeforces Round 32 Problem 888C - K-Dominant Character
1) Link to the problem: http://codeforces.com/contest/888/problem/C
2) Description:
You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant if 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.
3) 思路:
这题的tag居然是binary search,我做的时候基本没往这方面想。
我的思路就是算每个字母的maxgap然后求最小。
数据长度最长是10^5,然后26个字母注意:大小写要区分。
最后大概是O(n)左右,time limit是2s。所以肯定能过。O(n^2)的话就不用想了,O(n^2)属于不理智行为。
4) Code:
#include <iostream> #include <cstring> using namespace std; int main(){ string s; cin>>s; int n = s.size(); int min = INT_MAX; for(int i = 0; i < 52; i++) { char c; if(i<26) c = (char)('a' + i); else c = (char)('A' + i - 26); int count = 0; int maxgap = 0; for(int j = 0; j < n; j++){ if(s[j] == c) { if(maxgap < count) maxgap = count; count = 0; } else { count ++; } } if(maxgap < count) maxgap = count; if(maxgap < min) min = maxgap; if(min==0) break; } cout << min + 1; return 0; }