K-Dominant Character (模拟)
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
abacaba
2
zzzzz
1
abcde
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 (模拟)的更多相关文章
- 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 ...
- HDU 5122 K.Bro Sorting(模拟——思维题详解)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...
- [Tyvj1001]第K极值 (贪心?模拟)
考前打tyvj的水题 题目描述 给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字a[i]是小于maxlongint的非负整数 ,编程要求求出整个序列中第k大的数字减去 ...
- Codeforces Round #754 (Div. 2) C. Dominant Character
题目:Problem - C - Codeforces 如代码,一共有七种情况,注意不要漏掉 "accabba" , "abbacca" 两种情况: 使用 ...
- 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 ...
- 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 ...
- Longest Substring with At Most K Distinct Characters
Given a string, find the longest substring that contains only two unique characters. For example, gi ...
- [python3.5][PyUserInput]模拟鼠标和键盘模拟
一.PyUserInput安装 python3.5的PyMouse和PyKeyboard模块都集成到了PyUserInput模块中.在python3.5中,直接安装PyUserInput模块即可 Py ...
- 【2018暑假集训模拟一】Day1题解
T1准确率 [题目描述] 你是一个骁勇善战.日刷百题的OIer. 今天你已经在你OJ 上提交了y 次,其中x次是正确的,这时,你的准确率是x/y.然而,你最喜欢一个在[0; 1] 中的有理数p/q(是 ...
- 机器学习方法(七):Kmeans聚类K值如何选,以及数据重抽样方法Bootstrapping
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入.我的博客写一些自己用得到东西,并分享给 ...
随机推荐
- XSS自动化检测 Fiddler Watcher & x5s & ccXSScan 初识
一.标题:XSS 自动化检测 Fiddler Watcher & x5s & ccXSScan 初识 automated XSS testing assistant 二.引言 ...
- mysql函数之二:left,right,substring,substring_index MySQL截取字符串函数方法
函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...
- Lib之过?Java反序列化漏洞通用利用分析
转http://blog.chaitin.com/ 1 背景 2 Java反序列化漏洞简介 3 利用Apache Commons Collections实现远程代码执行 4 漏洞利用实例 4.1 利用 ...
- kubernetes 学习 创建cronjob
POM.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- 2016.6.18主窗体、子窗体InitializeComponent()事件、Load事件发生顺序以及SeleChanged事件的发生
主窗体,子窗体的InitializeComponent(构造函数).Load事件执行顺序 1.主窗体定义事件 new 主窗体() 构造函数进入主窗体InitializeComponent函数,该函数中 ...
- 2015.3.12 C#运用正则表达式点滴
Regex reg = new Regex(@"\w{1,}@\w{1,}\.(com)?(net)?"); //匹配(1个或多个)(字母或数字或下滑线)@ (1个或多个)(字母或 ...
- 谈谈开发文本转URL小工具的思路
URL提供了一种定位互联网上任意资源的手段,由于采用HTTP协议的URL能在互联网上自由传播和使用,所以能大行其道.在软件开发.测试甚至部署的环节,URL几乎可以说无处不再,其中用来定位文本的URL数 ...
- python 三元表达式、列表推导式、生成器表达式
一 三元表达式.列表推导式.生成器表达式 一 三元表达式 name=input('姓名>>: ') res='mm' if name == 'hahah' else 'NB' print( ...
- RPC: program not registered (ZT)
When we trying to use a particular RPC program, below may indicate that rpcbind is not running or t ...
- 类型:sqlserver;问题:版本;结果:sqlserver版本区分
LocalDB (SqlLocalDB)LocalDB 是 Express 的一种轻型版本,该版本具备所有可编程性功能,但在用户模式下运行,并且具有快速的零配置安装和必备组件要求较少的特点.如果您需要 ...