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

自己用两重for循环写了一个代码,提交的时候TEL
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm> using namespace std; int main()
{
struct nood
{
char c;
bool b;
} a[]; char s[];
scanf("%s", &s);
int len = strlen(s);
for(int i = ; i < len; i++)
{
a[i].c = s[i];
a[i].b = true;
} int flag1 = ;
int flag2 = ;
int maxn = ;
int ans[]; for(int i = ; i < len; i++)
{
if(a[i].b == true)
{
for(int j = i+; j < len; j++)
{
if(a[i].c == a[j].c)
{
maxn = max(maxn ,j - i - flag2);
flag2 = j - i;
a[j].b = false;
}
}
}
ans[flag1++] = maxn;
} sort(ans, ans+flag1); bool judge = true;
for(int i = ; i < len; i++)
{
if(!a[i].b)
judge = false;
} if(judge)
printf("%d\n", len/ + );
else
printf("%d\n", ans[]); return ;
}

 AC代码

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; vector <int> T[];
vector <int> res[];
const int INF=0x3f3f3f3f; int main(){
int len,tmp,t,ans=INF;
string s;
cin>>s;
len=s.size();
for(int i=;i<;i++) T[i].push_back(-);//最前面的处理
for(int i=;i<len;i++){
tmp=s[i]-'a';
t=i-T[tmp].back();
T[tmp].push_back(i);
res[tmp].push_back(t);
}
for(int i=;i<;i++){
t=len-T[i].back();//最后面的处理
res[i].push_back(t);
sort(res[i].begin(),res[i].end());
}
for(int i=;i<;i++){
if(res[i].size()>){
ans=min(ans,res[i].back());
}
}
if(ans==INF) cout<<len/+<<endl;
else cout<<ans<<endl;
return ;
}

K-Dominant Character (模拟)的更多相关文章

  1. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题

    Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...

  2. HDU 5122 K.Bro Sorting(模拟——思维题详解)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...

  3. [Tyvj1001]第K极值 (贪心?模拟)

    考前打tyvj的水题 题目描述 给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字a[i]是小于maxlongint的非负整数 ,编程要求求出整个序列中第k大的数字减去 ...

  4. Codeforces Round #754 (Div. 2) C. Dominant Character

    题目:Problem - C - Codeforces 如代码,一共有七种情况,注意不要漏掉  "accabba"  , "abbacca"  两种情况: 使用 ...

  5. Leetcode: Rearrange String k Distance Apart

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  6. Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  7. Longest Substring with At Most K Distinct Characters

    Given a string, find the longest substring that contains only two unique characters. For example, gi ...

  8. [python3.5][PyUserInput]模拟鼠标和键盘模拟

    一.PyUserInput安装 python3.5的PyMouse和PyKeyboard模块都集成到了PyUserInput模块中.在python3.5中,直接安装PyUserInput模块即可 Py ...

  9. 【2018暑假集训模拟一】Day1题解

    T1准确率 [题目描述] 你是一个骁勇善战.日刷百题的OIer. 今天你已经在你OJ 上提交了y 次,其中x次是正确的,这时,你的准确率是x/y.然而,你最喜欢一个在[0; 1] 中的有理数p/q(是 ...

  10. 机器学习方法(七):Kmeans聚类K值如何选,以及数据重抽样方法Bootstrapping

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入.我的博客写一些自己用得到东西,并分享给 ...

随机推荐

  1. 解决ListView 缓存机制带来的显示不正常问题

    ListView加载数据原理:系统绘制ListView时,首先会用getCount()函数得到要绘制的这个列表的长度,然后开始逐行绘制.然后调用getView()函数,在这个函数里面首先获得一个Vie ...

  2. [转载]proc_mkdir与proc_create

    1:创建proc文件夹struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent);参数1:na ...

  3. bash 中的行处理命令 awk

    转自:http://blog.chinaunix.net/uid-23302288-id-3785105.html

  4. 用Unreal Engine绘制实时CG影像

    转自:http://www.unrealchina.net/portal.php?mod=view&aid=225 近年来,对实时CG的关心热度越来越高,但要想弥补与预渲染方式的差异并不是那么 ...

  5. 火星坐标、百度坐标、WGS84坐标转换代码(JS)

    JS版本源码 /** * Created by Wandergis on 2015/7/8. * 提供了百度坐标(BD09).国测局坐标(火星坐标,GCJ02).和WGS84坐标系之间的转换 */ / ...

  6. phonegap制作windows phone包

    下载SDK win7及以下版本下载SDK http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=27570 WIN8下载SDK ht ...

  7. leetcode424

    public class Solution { public int CharacterReplacement(string s, int k) { int len = s.Length; ]; , ...

  8. TCP/IP 笔记 1.2 链 路 层

    都是包含三种类型.根据类型字段的值来进行区分 2.4 SLIP:串行线路IPS L I P的全称是Serial Line IP.它是一种在串行线路上对 I P数据报进行封装的简单形式,在RFC 105 ...

  9. Template Method模式和Strategy模式有何异同

    Template Method模式和Strategy模式有何异同 博客分类: 设计模式 Java  Template Method模式很容易理解,就是由基类提供一个模板,将各子类中不变的行为提取到基类 ...

  10. [Elasticsearch2.x] 多字段搜索 (三) - multi_match查询和多数字段 <译>

    multi_match查询 multi_match查询提供了一个简便的方法用来对多个字段执行相同的查询. NOTE 存在几种类型的multi_match查询,其中的3种正好和在“了解你的数据”一节中提 ...