zoukankan      html  css  js  c++  java
  • 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
    ", len/2 + 1);
        else
            printf("%d
    ", 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












    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    HAproxy 1.5 dev14 发布
    IBM/DW 使用 Java 测试网络连通性的几种方法
    Skype 4.1 Linux 发布,支持微软帐号登录
    Dorado 7.1.20 发布,Ajax的Web开发平台
    Aspose.Slides for Java 3.0 发布
    开发版本 Wine 1.5.18 发布
    BitNami Rubystack 开始支持 Ruby 2.0
    XWiki 4.3 正式版发布
    Silverlight实例教程 Out of Browser的Debug和Notifications窗口
    Silverlight实例教程 Out of Browser与Office的互操作
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8486475.html
Copyright © 2011-2022 走看看