zoukankan      html  css  js  c++  java
  • 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.

    Examples
    Input
    abacaba
    Output
    2
    Input
    zzzzz
    Output
    1
    Input
    abcde
    Output
    3
    读懂题意是第一步,每个字符存在一个子串长度k,每k长度的子串都包括这个字符,求最小k,计算每种字符的间距包括头和尾的最大值,然后求所有字符的最小值。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    char s[100001];
    int last[26],ml[26];///last记录字母上一个位置,ml更新单个字母间距最大值
    int main()
    {
        scanf("%s",s);
        int i = -1;
        while(s[++ i])
        {
            if(s[last[s[i] - 'a']] == s[i])ml[s[i] - 'a'] = max(i - last[s[i] - 'a'],ml[s[i] - 'a']);///由于last初始0,需要判断上个位置是否是相同字符
            else ml[s[i] - 'a'] = i + 1;///位置从0开始 需要加1代表长度
            last[s[i] - 'a'] = i;
        }
        int ans = 200000;
        for(int i = 0;i < 26;i ++)
        {
            if(s[last[i]] == 'a' + i)///字符串中存在这个字符
            {
                if(ml[i])ml[i] = max(ml[i],(int)strlen(s) - last[i]);
                else ml[i] = max(last[i] + 1,(int)strlen(s) - last[i]);///如果ml为0,就计算字符到开头和结尾中的最大值
                ans = min(ans,ml[i]);
            }
        }
        printf("%d",ans);
    }
  • 相关阅读:
    实习第三十天
    实习第二十九天
    武汉第二十七天
    实习第二十六天
    实习第二十五天
    实习第二十四天
    python基础之核心风格
    1 Python入门
    对计算机的基础概念讨论
    一对多,父对象包含其他对象字段时创建的连接就是一对多连接
  • 原文地址:https://www.cnblogs.com/8023spz/p/9002176.html
Copyright © 2011-2022 走看看