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\n" , result) ; 
    }
    
    return  0 ;
}
 

 

转载于:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7875824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值