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. [转载]将网卡(设备中断)绑定到特定CPU

    在前阵子看到HelloDB的一篇文章“MySQL单机多实例方案”中提到: 因为单机运行多个实例,必须对网络进行优化,我们通过多个的IP的方式,将多个MySQL实例绑定在不同的网卡上,从而提高整体的网络 ...

  2. 机器学习:逻辑回归(OvR 与 OvO)

    一.基础理解 问题:逻辑回归算法是用回归的方式解决分类的问题,而且只可以解决二分类问题: 方案:可以通过改造,使得逻辑回归算法可以解决多分类问题: 改造方法: OvR(One vs Rest),一对剩 ...

  3. Breaking Good

    Time limit2000 ms Memory limit262144 kB Breaking Good is a new video game which a lot of gamers want ...

  4. MySQL mysqldump备份与恢复

    1 用户权限 grant select,RELOAD,PROCESS,SUPER, REPLICATION CLIENT ON *.* TO 'bak'@'192.168.%' IDENTIFIED ...

  5. c# 使用GetPrivateProfileString 读ini数据 失败

    项目中用到 GetPrivateProfileString但是使用中, 发现 无法读出 ini 配置中的值, 比如Enable_log =3 我读到的是 API设置的默认值. 网上说可能时字符集编码的 ...

  6. 常见地图服务(WMS、WFS、WCS、TMS、WMTS

    1.网络地图服务(WMS) 网络地图服务(WMS)利用具有地理空间位置信息的数据制作地图.其中将地图定义为地理数据可视的表现.能够根据用户的请求返回相应的地图(包括PNG,GIF,JPEG等栅格形式或 ...

  7. JAVA input/output 流层次关系图

    在java中,input和output流种类繁多,那么它们之间是否有关系呢?答案是肯定的,其中使用到了设计模式,装饰模式 下图来自于HEAD FIRST 设计模式 装饰模式一章 下图来自网络博客:ht ...

  8. ElasticSearch入门一

    ElasticSearch入门一 1 安装ElasticSearch,配置环境变量,并且存在Java环境,而且是Java环境: 下图是安装的目录: 进入bin目录之后,请看bin目录: 启动elast ...

  9. 【总结整理】WebGIS学习-thinkGIS(地理常识):

    ##地图知识 ###地图定义 地图是按照一定的法则,有选择地以二维或多维形式与手段在平面或球面上表示地球(或其它星球)若干现象的图形或图像,它具有严格的数学基础.符号系统.文字注记,并能用地图概括原则 ...

  10. mongoDB的学习

    一:linux下安装mongoDB 1.在linux系统上安装MongoDB 上传安装包mongodb-linux-x86_64-3.0.6.tgz到linux系统的home目录下 tar -zxvf ...