Codeforce 888C - 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 kcontains 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

题意:

让你找一个最小的长度k,使得所有子串中存在一个相同的字符c


代码:
#include <iostream>
#include <string>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
bool f[100005];
int main() {
 string s;
 char c;
 int len;
 cin>>s;
 len=s.length();
 int mine=len/2+1,maxn,k=0;
 for(int i=0;i<len;i++){
  if(f[i]==0){
   c=s[i];
   f[i]=1;
   maxn=i+1;
   k=0;
   for(int j=i+1;j<len;j++){
    if(s[j]!=c) k++;
    else{
     f[j]=1;
     maxn=max(maxn,k+1);
     k=0;
    }
    if(s[j]!=c&&j==len-1) maxn=max(maxn,k+1);
   }
  }
  mine=min(maxn,mine);
 }
 cout<<mine<<endl;
 return 0;
}
posted @ 2017-11-13 21:11  红眼ll  阅读(272)  评论(0)    收藏  举报