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).
OutputPrint 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
读懂题意是第一步,每个字符存在一个子串长度k,每k长度的子串都包括这个字符,求最小k,计算每种字符的间距包括头和尾的最大值,然后求所有字符的最小值。
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; char s[100001]; int last[26],ml[26];///last记录字母上一个位置,ml更新单个字母间距最大值 int main() { scanf("%s",s); int i = -1; while(s[++ i]) { if(s[last[s[i] - 'a']] == s[i])ml[s[i] - 'a'] = max(i - last[s[i] - 'a'],ml[s[i] - 'a']);///由于last初始0,需要判断上个位置是否是相同字符 else ml[s[i] - 'a'] = i + 1;///位置从0开始 需要加1代表长度 last[s[i] - 'a'] = i; } int ans = 200000; for(int i = 0;i < 26;i ++) { if(s[last[i]] == 'a' + i)///字符串中存在这个字符 { if(ml[i])ml[i] = max(ml[i],(int)strlen(s) - last[i]); else ml[i] = max(last[i] + 1,(int)strlen(s) - last[i]);///如果ml为0,就计算字符到开头和结尾中的最大值 ans = min(ans,ml[i]); } } printf("%d",ans); }