代码改变世界

K-Dominant Character

2019-09-22 17:07  木木王韦  阅读(151)  评论(0)    收藏  举报

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.

Examples
Input
abacaba
Output
2
Input
zzzzz
Output
1
Input
abcde
Output
3
题意,在字符串s中找到一个数字k保证s字符串中k长度的子字符串都含有同一个字母。
方法:遍历a到z,然后依次找出出现本字母的最小长度(如果没有该字母,k的最小值是1,但是总会出现其他字符串中的字母更新或覆盖该k)。

#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;

int main(){
	string s;
	cin>>s;
	int l=s.size();
	char m='a';
	int k1=12345678;
	for(int i=0;i<26;i++){
		int k2=0;
		char ma=m+i;
		//cout<<ma<<" ";
		int link=1;
		for(int j=0;j<l;j++){
			if(s[j]==ma) link=1;
			else link++;
			k2=max(link,k2);
			//cout<<k2<<endl;
		}
		k1=min(k1,k2);
	}
	cout<<k1<<endl;
	return 0;
}