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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章