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;
    }
  • 相关阅读:
    session判断重复提交
    logback日志配置
    quartz动态job工具类 serviceh注入问题
    mysql10061登录失败错误解决方案
    安装步骤
    无法启动此程序,因为计算机丢失MSVCP120.dll
    Bigdecimal: Non-terminating decimal expansion; no exact representable decimal result.
    replace()函数用法
    随机读取表中一条数据
    oracle table()函数
  • 原文地址:https://www.cnblogs.com/rgvb178/p/7825873.html
Copyright © 2011-2022 走看看