Problem F. Wiki with String
Problem F. Wiki with String
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes
现在有一个字符串s, s中只包含数字字符0-9,现在需要输出一个子字符串k满足以下条件:
条件1: k包含0-9中所有的数字字符;
条件2:在所有符合要求的子字符串中, k的长度最小;
条件3:如果存在多个满足条件1和条件2的子字符串,请输出字典序最小的那个子字符串。
Input
输入一个字符串s,且s的长度不大于106
Output
输出符合要求的子字符串k;如果不存在符合要求的子字符串,请输出-1
Samples
| standard input | standard output |
| 00123489765 | 0123489765 |
| 1234567890123456789 | 0123456789 |
| 123456789123 | -1 |
思路:采用双指针和贪心的思想,利用set判断当前的子字符串是否符合条件
第一个指针从前到后扫描一遍,第二个指针贪心的往前移动
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set> using namespace std ; unordered_set<char> us ;
string s ;
int cnt[] ; int main(){ cin >> s ; int la = s.size(),l=,flag = 0x3f3f3f3f ;
int idx = ;
string tmp,ans ;
for(int i=;i<la;i++){
us.insert(s[i]) ;
cnt[s[i]-''] ++ ;
while(cnt[s[l]-'']>){//如果当前的子字符串内存在重复字符,贪心的让l++,因为新子字符串的长度一定比原子字符串的长度来的短
cnt[s[l]-''] -- ;
l++ ;
}
if(us.size() == ){
if(i-l+<flag){
flag = i-l+ ;
ans = s.substr(l,i-l+) ;
}else if(flag == i-l+){
tmp = s.substr(l,i-l+) ;
if(tmp<ans){
ans = tmp ;
}
}
idx ++ ;
}
}
if(idx){
cout << ans << endl ;
}else{
cout << - << endl ;
} return ; }
...
Problem F. Wiki with String的更多相关文章
- 实验12:Problem F: 求平均年龄
Home Web Board ProblemSet Standing Status Statistics Problem F: 求平均年龄 Problem F: 求平均年龄 Time Limit: ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem F
Problem F Funny Car Racing There is a funny car racing in a city with n junctions and m directed roa ...
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
- Problem F: Exponentiation
Problem F: ExponentiationTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 4 Solved: 2[Submit][Status][W ...
- Problem F: 合唱比赛开始了!
Problem F: 合唱比赛开始了! Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 440 Solved: 201[Submit][Status][ ...
- 几何入门合集 gym101968 problem F. Mirror + gym102082 Problem F Fair Chocolate-Cutting + gym101915 problem B. Ali and Wi-Fi
abstract: V const & a 加速 F. Mirror 题意 链接 问题: 有n个人在y=0的平面上(及xoz平面).z=0平面上有一面镜子(边平行于坐标轴).z=a平面上有q个 ...
- Problem F Plug It In!
题目链接:https://cn.vjudge.net/contest/245468#problem/F 大意:给你插座和电器的对应关系,有多个电器对应一个插座的情况,但是一个插座只能供一个电器使用,现 ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
题目: Problem F. Matrix GameInput file: standard inputOutput file: standard inputTime limit: 1 secondM ...
随机推荐
- JAVA 8 的新特性
Java8的新特性有:默认方法.函数式接口.lambda表达式.方法引用.Stream.Optional.日期API. 一.默认方法: 接口中的方法可以有方法体,但是必须要有static或者defau ...
- HTTP: Request中的post和get区别
* GET和POST之间的主要区别 1.GET是从服务器上获取数据,POST是向服务器传送数据. 2.在客户端, get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段 ...
- golang测试与性能调优
- Python与MogoDB交互
睡了大半天,终于有时间整理下拖欠的MongoDB的封装啦. 首先我们先进行下数据库的连接: conn = MongoClient('localhost',27017) # 建立连接 result = ...
- asp.net core 之中间件
Http请求资源的过程可以看成一个管道:“Pipe”,并不是所有的请求都是合法的.安全的,其于功能.性能或安全方面的考虑,通常需要在这管道中装配一些处理程序来筛选和加工这些请求.这些处理程序就是中间件 ...
- nginx+rsync实现本地yum源以及公网yum源
1.配置nginx的autoindex模块,开启目录浏览功能 2.使用rsync同步公网源上的软件包,至本地目录中 3.配置客户端指向即可 1.nginx提供目录浏览功能 [root@xuliangw ...
- JavaWeb 之 JSON
一.概述 1.概念 JSON:JavaScript Object Notation JavaScript对象表示法 2.基本格式 var p = {"name":"张三 ...
- 【RAC】 RAC For W2K8R2 安装--结尾篇(十)
[RAC] RAC For W2K8R2 安装--结尾篇(十) 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其 ...
- elasticsearch获取字段missing的数据
用head查询: demo如下 http://localhost:9200/sj_0505/lw_point_location/ _search post { "query": { ...
- python爬虫系列:三、URLError异常处理
1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的服务器 服务器不存在 在代码中,我们需要用try-except语句来包围并捕获相应的异常. ...