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.
The 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.
abacaba
2
zzzzz
1
abcde
3
题意:若任意s的长度为k的子串中都包含字母c,就把c称为k-dominant character.问k的最小值;
思路:首先,k的最大值肯定是长度的一半加一,这样最中间的字母就是k-dominant character,之后我们遍历一下每个字母个数大于一的字母,得到相邻两个相同字母的最大值,得到后,再让他和开头到第一个遍历字母的长度和最后一个字母到结尾的长度进行比较得到最大值,这个最终的最大值就是该字母的k,之后再比较一下每个字母的k得出最小值即可;
下面附上我的代码:
#include<bits/stdc++.h>
using namespace std;
int s[30];
char s1[100005];
int main()
{
cin>>s1;
int ams;
int n=strlen(s1);
for(int i=0;i<n;i++)
s[s1[i]-'a']++;
if(n%2)
ams=(n+1)/2;
else
ams=n/2+1;
for(int i=0;i<26;i++)
{
if(s[i]>1)
{
int ans=0;
int pos=0;
int sum=0;
int v,k;
for(int j=0;j<n;j++)
{
if(s1[j]==i+'a')
{
sum++;
if(sum==1)
v=j;
else if(sum==s[i])
k=j;
ans=max(j-pos,ans);
pos=j;
}
}
ans=max(ans,max(v+1,n-k));
ams=min(ans,ams);
}
}
printf("%d\n",ams);
return 0;
}