CodeForces 888C Dominant Character

Description:

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

题目大意:

求保证长度为n的s的每个长为k的子串都必须含有至少一个相同字符的最小k值。

解题思路:

顺序遍历字符串,对于每个字符记录它的下标, 再找到上一个出现的位置作差为距离, 对于同一种字符距离取最大值(注意要把第一个下标和最后一个下标设置为-1, 因为如果一个字母只出现过一次的话它的距离)。找所有字符中距离值最小的即可。

代码:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>

using namespace std;

/*
 *ios::sync_with_stdio(false);
 */

typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 1e6;

char arr[Max];
int dis[26], vis[26], cur[26];

int main() {
    // freopen("input.txt", "r", stdin);
    scanf("%s", arr);
    memset(cur, -1, sizeof(cur));
    memset(vis, 0 ,sizeof(vis));
    memset(dis, 0, sizeof(dis));
    int len = strlen(arr);
    for (int i = 0; i < len; ++i) {
        int index = arr[i] - 'a';
        dis[index] = max(dis[index], i - cur[index]);
        cur[index] = i;
        vis[index]++;
    }
    int temp[26];
    for (int i = 0; i < 26; ++i) {
        int index = len - cur[i];
        if (vis[i]) {
            dis[i] = max(dis[i], index);
        }
    }
    int ans = inf;
    for (int i = 0; i < 26; ++i) {
        if (vis[i]) {
            ans = min(dis[i], ans);
        }
    }
    printf("%d\n", ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值