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;
    }
  • 相关阅读:
    求数组元素出现的次数
    数组的一些内置方法
    二维数组
    创建对象
    取出数组最大值与最小值
    4-jQuery
    3-jQuery
    2-jQuery
    1-jQuery
    Spark共享变量(广播变量、累加器)
  • 原文地址:https://www.cnblogs.com/rgvb178/p/7825873.html
Copyright © 2011-2022 走看看