Educational Codeforces Round 32 C. K-Dominant Character(模拟)

叁歲伎倆 2022-06-06 13:25 269阅读 0赞

C. K-Dominant Character

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 kcontains 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

  1. abacaba

output

  1. 2

input

  1. zzzzz

output

  1. 1

input

  1. abcde

output

  1. 3

题解:

题意:

让你找一个最小的长度k,使得所有子串中存在一个相同的字符c

有点意思的一题,我的思路是记录下两个相同字符的最长距离,然后取这些距离的最小值就行了

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<queue>
  5. #include<stack>
  6. #include<math.h>
  7. #include<vector>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<cmath>
  12. #include<string>
  13. #include<algorithm>
  14. #include<iostream>
  15. using namespace std;
  16. #define lson k*2
  17. #define rson k*2+1
  18. #define M (t[k].l+t[k].r)/2
  19. #define ll long long
  20. #define INF 10086111
  21. int a[30];
  22. int b[30];
  23. int c[30];
  24. int main()
  25. {
  26. char s[100005];
  27. int i,t;
  28. memset(b,0,sizeof(b));
  29. for(i=0;i<26;i++)
  30. a[i]=-1;
  31. memset(c,0,sizeof(c));
  32. scanf("%s",s);
  33. for(i=0;s[i];i++)
  34. {
  35. t=s[i]-'a';
  36. b[t]=max(b[t],i-a[t]);
  37. a[t]=i;
  38. c[t]++;
  39. }
  40. for(i=0;i<26;i++)
  41. {
  42. int temp=strlen(s)-a[i];
  43. if(c[i])
  44. b[i]=max(b[i],temp);
  45. }
  46. int minn=1008611;
  47. for(i=0;i<26;i++)
  48. {
  49. if(c[i]!=0)
  50. minn=min(minn,b[i]);
  51. }
  52. printf("%d\n",minn);
  53. return 0;
  54. }

发表评论

表情:
评论列表 (有 0 条评论,269人围观)

还没有评论,来说两句吧...

相关阅读