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. MSSQL2008 常用sql语句

    一.基础 1.说明:创建数据库 Create DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...

  2. Qt中如何用QImage::Format_Indexed8表示灰度图

    QImage *qi = new QImage(data_ptr, width, height, QImage::Format_Indexed8); QVector<QRgb> grayT ...

  3. Java-API-POI-Excel:HSSFWorkbook Documentation

    ylbtech-Java-API-POI-Excel:HSSFWorkbook Documentation 1.返回顶部 1. org.apache.poi.hssf.usermodel Class ...

  4. Regexp:教程

    ylbtech-Regexp:教程 1.返回顶部 1. 正则表达式 - 教程 正则表达式(Regular Expression)是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符( ...

  5. VisualGDB系列11:Linux C++项目中使用外部Linux库

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 在<使用VS创建Linux静态库和 ...

  6. 2015.1.10 解决DataGridView SelectionChanged事件自动触发问题

    DataGridView SelectionChanged事件总是在数据源更改时自动触发,这点很讨厌. 可用CellClick和KeyUp事件和一个函数替代SelectionChanged事件 pri ...

  7. 为什么in_array(0, ['a', 'b', 'c'])返回true

    为什么in_array(0, ['a', 'b', 'c'])返回true 目录 1 类型转换 2 严格比较 3 false和null 4 数组中有true 在PHP中,数据会自动转换类型后进行比较. ...

  8. 如何使用安装光盘为本机创建yum repository

    在CentOS 6上可以使用系统安装光盘为本机创建yum repository,创建过程如下. 创建光盘mount点 [root@centos62 ~]# mkdir /media/CentOS mo ...

  9. Shell编程进阶 1.6 if判断的几种用法

    针对文件和目录的逻辑判断 touch .txt .txt ]; then echo ok;fi -f 判断1.txt是否是文件且是否存在,成立输出ok if [-d /tmp/ ]; then ech ...

  10. LAMP 2.7 Apache通过rewrite限制某个目录

    我们可以 allow 和 deny 去现在网站根目录下的某个子目录,当然这个 rewrite 也可以实现,配置如下: 创建一个目录和文件随便写些东西 mkdir /data/www/data/tmp ...