zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 32 Problem 888C

    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 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;
    }
  • 相关阅读:
    cf1100 F. Ivan and Burgers
    cf 1033 D. Divisors
    LeetCode 17. 电话号码的字母组合
    LeetCode 491. 递增的子序列
    LeetCode 459.重复的子字符串
    LeetCode 504. 七进制数
    LeetCode 3.无重复字符的最长子串
    LeetCode 16.06. 最小差
    LeetCode 77. 组合
    LeetCode 611. 有效三角形个数
  • 原文地址:https://www.cnblogs.com/rgvb178/p/7825873.html
Copyright © 2011-2022 走看看