题意:给你一个字符串,问是否存在一个长度为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 贪心的更多相关文章

  1. 2019HDU多校第一场1001 BLANK (DP)(HDU6578)

    2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...

  2. [2019HDU多校第一场][HDU 6578][A. Blank]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题目大意:长度为\(n\)的数组要求分别填入\(\{0,1,2,3\}\)四个数中的任意一个,有 ...

  3. [2019HDU多校第一场][HDU 6580][C. Milk]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6580 题目大意:\(n\times m\)大小的方格上有\(k\)瓶水,喝完每瓶水都需要一定的时间.初 ...

  4. [2019HDU多校第一场][HDU 6584][G. Meteor]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6584 题目大意:求所有满足\(0<\frac{p}{q}\leq1, gcd(p,q)=1,p\ ...

  5. [2019HDU多校第一场][HDU 6590][M. Code]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线 ...

  6. [2019HDU多校第一场][HDU 6588][K. Function]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6588 题目大意:求\(\sum_{i=1}^{n}gcd(\left \lfloor \sqrt[3] ...

  7. 2019HDU多校第一场 BLANK DP

    题意:有四种数字,现在有若干个限制条件:每个区间中不同的数字种类必须是多少种,问合法的方案数. 思路: 定义 dp[i][j][k][t] 代表填完前 t 个位置后,{0,1,2,3} 这 4 个数字 ...

  8. 2019HDU多校第一场 6582 Path 【最短路+最大流最小割】

    一.题目 Path 二.分析 首先肯定要求最短路,然后如何确定所有的最短路其实有多种方法. 1 根据最短路,那么最短路上的边肯定是可以满足$dist[from] + e.cost = dist[to] ...

  9. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

随机推荐

  1. linux 性能测试之基准测试工具

    https://niyunjiu.iteye.com/blog/316302 system: lmbench unixbench5.1.2 ubench freebench nbench ltp xf ...

  2. 接口需要上一个接口的返回值(unittest)

    import unittest,requests ''' 使用unittest框架的时候,这个接口需要上一个接口的返回值 ''' class Test_case(unittest.TestCase): ...

  3. 算法 识别有效ip地址和掩码并做统计

    题目描述 请解析IP地址和对应的掩码,进行分类识别.要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类. 所有的IP地址划分为 A,B,C,D,E五类 A类地址1.0.0.0~126.2 ...

  4. Static Fields and Methods

    If you define a field as static, then there is only one such field per class. In contrast, each obje ...

  5. nyoj 78:圈水池 【凸包入门】

    题目链接 将所有点按从左至右顺序排序,然后将所有点先从左到右扫描再从右到左扫描,逐渐将凸包轮廓“勾勒”出来 (凸包轮廓满足,轮廓上连续的三个点按先后顺序给出的话呈逆时针方向) 最后删去一个重复的起(终 ...

  6. 解决如何通过循环来使用数据库的值设置jsp的select标签的option值

    Select 处的代码: <select name="position"> <span style="white-space:pre"> ...

  7. STM32 TIM3 PWM输出 4路

    一.设置TIM3的GPIO为推挽输出 void TIM3_IOConfig(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClo ...

  8. window 安装iis和使用

    一.IIS安装(服务名World Wide Web Publishing Service) 实验环境:win2008R2虚拟机1.首先打开虚拟机,然后选中管理工具->服务器管理器. 2.选中角色 ...

  9. [CSP-S模拟测试]:联(小清新线段树)

    题目描述 由于出题人懒所以没有背景.一个无限长的$01$序列,初始全为$0$,每次选择一个区间$[l,r]$进行操作,有三种操作:$\bullet 1\ l\ r$将$[l,r]$中所有元素变成$1$ ...

  10. Unity Shader之模板测试

    Unity Shader之模板测试 一沙一世界,一花一天堂 一.Stencil testing 渲染管线     当片段着色器处理完一个片段之后,模板测试(Stencil Test)会开始执行,和深度 ...