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

自己用两重for循环写了一个代码,提交的时候TEL
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    struct nood
    {
        char c;
        bool b;
    } a[100005];

    char s[100005];
    scanf("%s", &s);
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
        a[i].c = s[i];
        a[i].b = true;
    }

    int flag1 = 0;
    int flag2 = 0;
    int maxn = 0;
    int ans[30];

    for(int i = 0; i < len; i++)
    {
        if(a[i].b == true)
        {
            for(int j = i+1; j < len; j++)
            {
                if(a[i].c == a[j].c)
                {
                    maxn = max(maxn ,j - i - flag2);
                    flag2 = j - i;
                    a[j].b = false;
                }
            }
        }
        ans[flag1++] = maxn;
    }

    sort(ans, ans+flag1);

    bool judge = true;
    for(int i = 0; i < len; i++)
    {
        if(!a[i].b)
            judge = false;
    }

    if(judge)
        printf("%d\n", len/2 + 1);
    else
        printf("%d\n", ans[0]);

    return 0;
}
View Code

 AC代码

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

vector <int> T[30];
vector <int> res[30];
const int INF=0x3f3f3f3f;

int main(){
    int len,tmp,t,ans=INF;
    string s;
    cin>>s;
    len=s.size();
    for(int i=0;i<26;i++) T[i].push_back(-1);//最前面的处理
    for(int i=0;i<len;i++){
        tmp=s[i]-'a';
        t=i-T[tmp].back();
        T[tmp].push_back(i);
        res[tmp].push_back(t);
    }
    for(int i=0;i<26;i++){
        t=len-T[i].back();//最后面的处理
        res[i].push_back(t);
        sort(res[i].begin(),res[i].end());
    }
    for(int i=0;i<26;i++){
        if(res[i].size()>1){
            ans=min(ans,res[i].back());
        }
    }
    if(ans==INF) cout<<len/2+1<<endl;
    else cout<<ans<<endl;
    return 0;
}
View Code

 

 













posted @ 2018-03-01 00:15  Veritas_des_Liberty  阅读(345)  评论(0编辑  收藏  举报