F - 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.

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.

Example
Input
abacaba
Output
2
Input
zzzzz
Output
1
Input
abcde
Output
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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值