Codeforces 888C: K-Dominant Character(水题) Input Output Examples 题意 思路 代码
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).
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
题意
给出一个字符串,找出一个最小的长度(k),使得每个长度为(k)的子串中都包含一个相同的字符
思路
记录下来每个字符的位置,找两个相同字符的最大距离,对这个最大距离取最小值
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin>>s;
int l=s.length();
vector<int>ve[30];
for(int i=0;i<26;i++)
ve[i].push_back(-1);
for(int i=0;i<l;i++)
ve[s[i]-'a'].push_back(i);
for(int i=0;i<26;i++)
ve[i].push_back(l);
int ans=inf;
for(int i=0;i<26;i++)
{
int res=0;
int sz=ve[i].size();
for(int j=1;j<sz-1;j++)
res=max(res,max(ve[i][j]-ve[i][j-1],ve[i][j+1]-ve[i][j]));
if(res==0)
continue;
ans=min(ans,res);
}
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}