Codeforces 888C: K-Dominant Character(水题)

本文介绍了一种算法,用于找出字符串中每个长度为k的子串都包含相同字符的最小k值。通过记录每个字符出现的位置,并计算两个相同字符间的最大距离来确定k值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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\)的子串中都包含一个相同的字符

思路

记录下来每个字符的位置,找两个相同字符的最大距离,对这个最大距离取最小值

代码

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        srand((unsigned int)time(NULL));
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin>>s;
    int l=s.length();
    vector<int>ve[30];
    for(int i=0;i<26;i++)
        ve[i].push_back(-1);
    for(int i=0;i<l;i++)
        ve[s[i]-'a'].push_back(i);
    for(int i=0;i<26;i++)
        ve[i].push_back(l);
    int ans=inf;
    for(int i=0;i<26;i++)
    {
        int res=0;
        int sz=ve[i].size();
        for(int j=1;j<sz-1;j++)
            res=max(res,max(ve[i][j]-ve[i][j-1],ve[i][j+1]-ve[i][j]));
        if(res==0)
            continue;
        ans=min(ans,res);
    }
    cout<<ans<<endl;
    #ifndef ONLINE_JUDGE
        cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
    #endif
    return 0;
}

转载于:https://www.cnblogs.com/Friends-A/p/11568882.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值