题目链接:http://codeforces.com/problemset/problem/888/C
题目:
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
题意:字符串任意k长度子串都要包含某个字母,问长度k最小为多少。
题解:先预处理把相同字母之间的长度都存到一个数组里(这里注意最前面和最后面的处理),都要包含某个字母,那么我们肯定要取数组里面的最大值,然后把最大值比较一下,取下最小值就OK啦。
1 #include <vector> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 6 vector <int> T[30]; 7 vector <int> res[30]; 8 const int INF=0x3f3f3f3f; 9 10 int main(){ 11 int len,tmp,t,ans=INF; 12 string s; 13 cin>>s; 14 len=s.size(); 15 for(int i=0;i<26;i++) T[i].push_back(-1);//最前面的处理 16 for(int i=0;i<len;i++){ 17 tmp=s[i]-‘a‘; 18 t=i-T[tmp].back(); 19 T[tmp].push_back(i); 20 res[tmp].push_back(t); 21 } 22 for(int i=0;i<26;i++){ 23 t=len-T[i].back();//最后面的处理 24 res[i].push_back(t); 25 sort(res[i].begin(),res[i].end()); 26 } 27 for(int i=0;i<26;i++){ 28 if(res[i].size()>1){ 29 ans=min(ans,res[i].back()); 30 } 31 } 32 if(ans==INF) cout<<len/2+1<<endl; 33 else cout<<ans<<endl; 34 return 0; 35 }
时间: 2024-10-11 20:43:18