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);
    }
  • 相关阅读:
    高斯函数 and 对数高斯函数 拟合曲线
    Python3.6、3.7、3.8新特性
    常用限流策略——漏桶与令牌桶介绍
    常用的HTTP服务压测工具介绍
    使用swagger生成接口文档
    validator库参数校验若干实用技巧
    树形结构数据处理
    element-ui 抽屉组件(el-drawer ) 二次封装 增加resize拖曳改变宽度大小
    pm2常用命令
    vue3-- setup中获取数组dom
  • 原文地址:https://www.cnblogs.com/8023spz/p/9002176.html
Copyright © 2011-2022 走看看