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 ...
随机推荐
- C语言 小球撞击反弹
计算法(略) #include <stdio.h> #include <stdlib.h> int main() { int x, y, a, resu; scanf(&quo ...
- java知识精要(一)
一.java数组 (疯狂java讲义 第4.5 ~ 4.6章节) 1) 声明形式: type[] arrayName; 推荐方式 type arrayName[]; 2) 初始化: 方式一: type ...
- IDEA中使用git
这篇文章写的很好,记录参考 [转载] https://blog.csdn.net/autfish/article/details/52513465 https://www.jianshu.com/p/ ...
- Linux磁盘设备基础
free -m 查看系统内存 [root@zhang /]# free -m total used free shared buffers cached ...
- 创建maven父项目以及子项目
创建maven父项目以及子项目(Eclipse创建Maven Project跟Maven Module)https://blog.csdn.net/Mrsanger/article/details/8 ...
- NEST 自定义分析器
public void SetAnalysis() { if (!client.IndexExists("employee").Exists) { client.CreateInd ...
- 2019三六零 java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.360等公司offer,岗位是Java后端开发,最终选择去了三六零. 面试了很多家公司,感觉大部分公司考察的点都差 ...
- 【夯实基础】- https和http的主要区别
HTTPS和HTTP的区别主要如下: 1.https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用. 2.http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输 ...
- 【开发笔记】- 发送邮件(通过JavaMail发送)
前段时间在工作中用到了邮件发送监控的报警信息,今天在这个记录一下JavaMail的邮件工具类. 下边为用到的JavaMail的jar包的pom依赖.这里用的是JavaMail的1.4.6的版本. &l ...
- 非洲affrike单词
affrike 英文单词,含义是非洲,非洲大陆. 中文名:非洲 外文名:affrike 目录 释义 affrike noun名词 非洲,也用做africa 1.Word Origin and Hist ...