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)
  • 相关阅读:
    软工随堂小测——四则运算
    K米测评
    uml用例图
    第二次结对编程作业——毕设导师智能匹配
    原型设计与需求分析
    散列函数的应用及其安全性
    Think In Java读书笔记:内部类覆盖及其初始化
    如何在服务器(centOS系统)上运行JavaWeb项目
    结对工程作业
    《构建之法》——第四、第十七章
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8486475.html
Copyright © 2011-2022 走看看