CodeForces 888C K-Dominant Character(模拟)

Posted Farewell

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 888C 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

题意:

任意长度至少为k的子串包含有相同的字母。

题解:

从a到z找一遍就好了,寻找同一字母在序列中出现的最大的间隔,最后取间隔最小的字母。

自己写的虽然过了,感觉代码写的很烂。

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
    string s;
    while(cin>>s)
    {
        int last[26];
        int a[26]={0},len=s.length();
        memset(last,-1,sizeof(last));
        for(int i=0;i<len;i++)
        {
            int t=s[i]-'a';
            if(last[t]==-1)
            {
                a[t]=i+1;
                last[t]=i;
            }
            else
            {
                a[t]=max(a[t],i-last[t]);
                last[t]=i;
            }
        }
        int ans=100000;
        for(int i=0;i<26;i++)//处理到字符串结尾的间隔
        {           
            if(last[i]!=-1)
            {
                a[i]=max(a[i],len-last[i]);
                ans=min(ans,a[i]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

参考别人重新写的

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string s;
    while(cin>>s)
    {
        int ans=1e6;
        for(int i='a';i<='z';i++)
        {
            int t=0,k=0;
            for(int j=0;j<s.length();j++)
            {
                if(s[j]==i)
                    t=0;
                else
                    t++;
                k=max(k,t);
            }
            ans=min(ans,k);
        }
        cout<<ans+1<<endl;
    }
    return 0;
}

以上是关于CodeForces 888C K-Dominant Character(模拟)的主要内容,如果未能解决你的问题,请参考以下文章

Error response from daemon: conflict: unable to delete 31f279e888c0 (must be forced) - image is bein

codeforces上怎么看测试数据

如何看codeforces做了多少题

codeforces上怎么看测试数据

codeforces比赛后怎么看题解和答案

codeforces是啥?