题目链接---K-Dominant Character
K-Dominant Character
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.
InputThe first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).
Print one number — the minimum value of k such that there exists at least one k-dominant character.
Input
abacaba
Output
2
Input
zzzzz
Output
1
Input
abcde
Output
3
题意:一个字符串,每k个字符都会出现相同的字符c ( c 不确定),求最小的k
题解:先求得两个相同字符的之间的最大值,最后找出最小的那个
#include<bits/stdc++.h>
using namespace std;
string str;
int main()
{
cin>>str;
int ans=1e6;
for(int i='a';i<='z';i++)
{
int k=0,t=1;
for(int j=0;j<str.size();j++)
{
if(str[j]==i)
t=1;
else
t++;
k=max(k,t);//找出相同的两个字符之间的最大值
}
ans=min(ans,k);//最后找出一个最小的
}
cout<<ans<<endl;
return 0;
}