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)
  • 相关阅读:
    分层图最短路(DP思想) BZOJ2662 [BeiJing wc2012]冻结
    动态规划 BZOJ1925 地精部落
    线性DP SPOJ Mobile Service
    线性DP codevs2185 最长公共上升子序列
    数位DP POJ3208 Apocalypse Someday
    线性DP POJ3666 Making the Grade
    杨氏矩阵 线性DP? POJ2279 Mr.Young's Picture Permutations
    tarjan强连通分量 洛谷P1262 间谍网络
    树链剖分 BZOJ3589 动态树
    二分图 BZOJ4554 [Tjoi2016&Heoi2016]游戏
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8486475.html
Copyright © 2011-2022 走看看