• Educational Codeforces Round 32 C题 K-Dominant Character


    C. K-Dominant Character
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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

     题意:给一个字符串 , 从头开始向后每次依次 截取 一定长度的 字符串,使得 每个字符串里都要有 ,相同的字符 ,

               问  截取的长度 最小为多少 ?

    思路:对于每一个字符来说 更新 所需的 最大长度   ,  最后对所有的 26 个字符 取最小的 长度

     

    #include <cstdio>
    #include <iostream> 
    #include <algorithm>
    #include <cstring>
    
    using namespace std ; 
    
    #define maxn 110000
    char str[maxn] ; 
    int pos_front[30] ; 
    int dis_pos[30] ; 
    bool visit[30] ; 
    int main(){
        
        while(~scanf(" %s" , str)){
            for(int i =0 ; i< 30 ; i++){
                pos_front[i] = -1 ; 
            }
            
            memset(dis_pos , 0 , sizeof(dis_pos)) ; 
            memset(visit , false , sizeof(visit)) ; 
            
            int len  =strlen(str) ; 
            // 向前 检测  每一个出现过的字符的  最小必须截断长度 
            for( int i=0 ;  i< len ; i++){
                dis_pos[str[i] -'a'] =  max(i - pos_front[str[i]-'a'] , dis_pos[str[i] -'a'] ) ; 
                pos_front[str[i]-'a'] = i ; 
                visit[str[i]-'a'] = true ;     
            }
            // 尾部  向后 检测 每一个出现过的字符的截断长度  并更新  这个字符在整个字符串中的最小必须截断长度 
            for(int i=0 ; i<26 ; i++){
                if(visit[i]){
                    dis_pos[i] = max(dis_pos[i] , len - pos_front[i] ) ; 
                }
            }
            
            int result = maxn ; 
            bool flag = false  ; 
            for(int i=0 ; i<26 ; i++){
                if(dis_pos[i] != 0 ){
                    result = min(result , dis_pos[i]) ; 
                }
            } 
                printf("%d
    " , result) ; 
        }
        
        return  0 ;
    }
     
  • 相关阅读:
    对软件工程的理解及问题
    使用Junit等工具进行单元测试
    软件工程
    进销存管理系统——可行性分析
    使用Junit等工具进行单元测试
    两个人的分组
    物联网软件工程 认识与问题
    二人项目
    使用Junit等工具进行单元测试
    软件工程
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7875824.html
Copyright © 2020-2023  润新知