2019HDU多校第一场 String 贪心
题意:给你一个字符串,问是否存在一个长度为m的子序列,子序列中对应字符的数目必须在一个范围内,问是否存在这样的字符串?如果存在,输出字典序最小的那个。
思路:贪心,先构造一个序列自动机,序列自动机指向在它后面离它最近的某个字符的位置。对于当前位置,从a开始枚举字符,如果答案串的下个位置填这个字符可以,就立马填上这个字符,最后看一下贪心构造的字符串长度是不是m就可以了。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int cnt[26], last[26], l[26], r[26];
int ssum[maxn][26], Next[maxn][26];
int n, m, tot;
char s[maxn], ans[maxn];
bool valid(int x) {
if(x == n + 1) {
for (int i = 0; i < 26; i++) {
if(cnt[i] >= l[i] && cnt[i] <= r[i]) continue;
return 0;
}
if(tot != m) return 0;
return 1;
}
bool flag = 1;
cnt[s[x] - 'a']++;
tot++;
for (int i = 0; i < 26; i++) {
if(ssum[x + 1][i] + cnt[i] >= l[i] && cnt[i] <= r[i]) continue;
else flag = 0;
}
int tmp = 0;
for (int i = 0; i < 26; i++) {
tmp += max(l[i] - cnt[i], 0);
}
if(m - tot > n - x + 1) flag = 0;
if(m - tot < tmp) flag = 0;
if(flag == 0) {
tot--;
cnt[s[x] - 'a']--;
return 0;
}
return 1;
}
int main() {
while(~scanf("%s", s + 1)) {
tot = 0;
scanf("%d", &m);
for (int i = 0; i < 26; i++) {
scanf("%d%d", &l[i], &r[i]);
}
n = strlen(s + 1);
memset(ssum[n + 1], 0, sizeof(ssum[n + 1]));
memset(cnt, 0, sizeof(cnt));
for (int j = 0; j < 26; j++) last[j] = n + 1;
for (int i = n; i >= 1; i--) {
for (int j = 0; j < 26; j++) {
ssum[i][j] = ssum[i + 1][j];
Next[i][j] = last[j];
}
ssum[i][s[i] - 'a']++;
last[s[i] - 'a'] = i;
}
for (int i = 0; i < 26; i++)
Next[0][i] = last[i];
int pos = 0;
while(pos <= n && tot < m) {
bool flag = 0;
for (int j = 0; j < 26; j++) {
if(valid(Next[pos][j])) {
ans[tot] = 'a' + j;
pos = Next[pos][j];
flag = 1;
break;
}
}
if(flag == 0) break;
}
if(tot == m) {
ans[tot + 1] = 0;
printf("%s\n", ans + 1);
} else {
printf("-1\n");
}
}
}
2019HDU多校第一场 String 贪心的更多相关文章
- 2019HDU多校第一场1001 BLANK (DP)(HDU6578)
2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...
- [2019HDU多校第一场][HDU 6578][A. Blank]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题目大意:长度为\(n\)的数组要求分别填入\(\{0,1,2,3\}\)四个数中的任意一个,有 ...
- [2019HDU多校第一场][HDU 6580][C. Milk]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6580 题目大意:\(n\times m\)大小的方格上有\(k\)瓶水,喝完每瓶水都需要一定的时间.初 ...
- [2019HDU多校第一场][HDU 6584][G. Meteor]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6584 题目大意:求所有满足\(0<\frac{p}{q}\leq1, gcd(p,q)=1,p\ ...
- [2019HDU多校第一场][HDU 6590][M. Code]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线 ...
- [2019HDU多校第一场][HDU 6588][K. Function]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6588 题目大意:求\(\sum_{i=1}^{n}gcd(\left \lfloor \sqrt[3] ...
- 2019HDU多校第一场 BLANK DP
题意:有四种数字,现在有若干个限制条件:每个区间中不同的数字种类必须是多少种,问合法的方案数. 思路: 定义 dp[i][j][k][t] 代表填完前 t 个位置后,{0,1,2,3} 这 4 个数字 ...
- 2019HDU多校第一场 6582 Path 【最短路+最大流最小割】
一.题目 Path 二.分析 首先肯定要求最短路,然后如何确定所有的最短路其实有多种方法. 1 根据最短路,那么最短路上的边肯定是可以满足$dist[from] + e.cost = dist[to] ...
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
随机推荐
- lik模糊e查询语句,索引使用效果详解
一.like查询与索引 在oracle里的一个超级大的表中,我们的where条件的列有建索引的话,会走索引唯一扫描INDEX UNIQUE SCAN.如select * from table wher ...
- source insight 4.0.86.0安装破解问题
source insight 4.0.86.0安装过程中碰到导入lic文件一直不正确 解决办法: 需要将SourceInsight\SW_Install\SI4安装及破解文件 目录下的sourcein ...
- Servlet接口和web.xml配置文件初识
一.Java Web核心Servlet 1.什么是Servlet? Servlet是运行在服务器端的Java小程序,是sun公司提供的一套规范,用来处理客户端请求.响应给浏览器的动态资源.但Servl ...
- ES5和ES6数组方法
ES5 方法 indexOf和lastIndexOf 都接受两个参数:查找的值.查找起始位置不存在,返回 -1 :存在,返回位置.indexOf 是从前往后查找, lastIndexOf 是从后往前查 ...
- Erlang/Elixir精选-第4期(20191223)
精选文章 A digital symphony - The architecture of API Fortress. 使用Actor模型来支持基于微服务的大规模分布式软件架构.用实例解释了Actor ...
- 前端-PC端瀑布流【10张图】
.HTML 利用封装的 jquerywaterfall.js 方法 完成 <!DOCTYPE html> <html lang="en"> <head ...
- Apache Flink 进阶(六):Flink 作业执行深度解析
本文根据 Apache Flink 系列直播课程整理而成,由 Apache Flink Contributor.网易云音乐实时计算平台研发工程师岳猛分享.主要分享内容为 Flink Job 执行作业的 ...
- Java的Object几个重写的方法
1:toString(); 只是简单的列出对象的状态(也就是重要的实例变量的当前值). package jicheng;public class Animal { public static void ...
- RHEL 使用epel源
转自http://www.linuxidc.com/Linux/2012-10/71850.htm 设置epel源.方法如下: 32位系统选择:rpm -ivh http://download.Fed ...
- (转)springboot应用启动原理(二) 扩展URLClassLoader实现嵌套jar加载
转:https://segmentfault.com/a/1190000013532009 在上篇文章<springboot应用启动原理(一) 将启动脚本嵌入jar>中介绍了springb ...