C. K-Dominant Character
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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
Copy
abacaba
output
2
input
Copy
zzzzz
output
1
input
Copy
abcde
output
3
让求一个最小的切割距离使得每段都有重复的字母,转化为求每个字母最大间距的最小值,注意边界。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <string>
using namespace std;
typedef long long ll;
int main(){
string s;
cin>>s;
int len=s.length();
map<char,int> mm;
map<char,int> mmx;
for(int i=0;i<26;i++){
char c='a'+i;
mm[c]=-1;
mmx[c]=0;
}
for(int i=0;i<len;i++){
mmx[s[i]]=max(mmx[s[i]],i-mm[s[i]]);
mm[s[i]]=i;
}
int ans=len;
for(int i=0;i<26;i++){
char c='a'+i;
//cout<<mmx[c]<<endl;
mmx[c]=max(mmx[c],len-mm[c]);
ans=min(ans,mmx[c]);
}
cout<<ans<<endl;
return 0;
}