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

题解:

看了一篇题解,发现那位大佬真的是把这道题当成思维题了(一直坚信暴力+思维出奇迹!!),因为看到其他大佬都是用二分和其他高深算法写的。

题意:给你一个字符串,找出一个字符x(未知),保证在长度为k的子串中,字符x一定出现。字符x你可以任意找,但是你找的k值一定要是最小的。

解题思路:题中给的字母是小写字母,小写字母是在26个之内的。我们一个字母一个字母查询,找到字符串中每一个所包括的小写字母的k值,最后再选出最小的k出来。

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <map>

using namespace std;

int main()
{
//	freopen("input.txt","r",stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	string s;
	cin>>s;
	int len=s.length();
	char m='a';
	int k3=12345678;
	for(int i=0;i<26;i++)
	{
		char n=m+i;
		int k1=1;
		int k2=-1;
		for(int j=0;j<len;j++)
		{
			if(n==s[j]) k1=1;
			else k1++;
			k2=max(k2,k1);
		}
		k3=min(k3,k2);	
	}
	cout<<k3;
	
	return 0;
}
原创文章 99 获赞 15 访问量 7355

猜你喜欢

转载自blog.csdn.net/qq_45328552/article/details/101226683