题目描述

Tom has a string containing only lowercase letters. He wants to choose a subsequence of the string whose length is k and lexicographical order is the smallest. It's simple and he solved it with ease.
But Jerry, who likes to play with Tom, tells him that if he is able to find a lexicographically smallest subsequence satisfying following 26 constraints, he will not cause Tom trouble any more.
The constraints are: the number of occurrences of the ith letter from a to z (indexed from 1 to 26) must in [Li,Ri].
Tom gets dizzy, so he asks you for help.

 

输入

The input contains multiple test cases. Process until the end of file.
Each test case starts with a single line containing a string S(|S|≤105)and an integer k(1≤k≤|S|).
Then 26 lines follow, each line two numbers Li,Ri(0≤Li≤Ri≤|S|). 
It's guaranteed that S consists of only lowercase letters, and ∑|S|≤3×105.

输出

Output the answer string.
If it doesn't exist, output −1.

样例输入

aaabbb 3
0 3
2 3
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

样例输出

abb
题解:
将字母下标按字母从前往后依次抽取出来;
然后从前往后依次确定k的每一位应该放的字母;
对于每一位,枚举字母的顺序应该是从a到z,接着判断该字母放上去后是否符合条件,符合则去确定k的下一位字母,不符合则继续循环;
时间复杂度应该是O(26*26*n)。
AC代码:
 #include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int suf_sum[maxn][],l[],r[],used[],n,k,last;
char str[maxn],res[maxn];
vector<int> letter_index[];
int main()
{
while(scanf("%s %d",str,&k)!=EOF)
{
for(int i=;i<=;i++){
scanf("%d %d",&l[i],&r[i]);
letter_index[i].clear();
}
vector<int> ::iterator head[];
memset(used,,sizeof(used));
n=strlen(str);last=-;
for(int i=;i<=n;i++) for(int j=;j<=;j++) suf_sum[i][j]=;
for(int i=n-;i>=;i--){
for(int j=;j<=;j++){
if(str[i]=='a'+j) suf_sum[i][j]=suf_sum[i+][j]+;
else suf_sum[i][j]=suf_sum[i+][j];
}
}
for(int i=;i<=n-;i++){
letter_index[str[i]-'a'].push_back(i);
}
for(int i=;i<=;i++){
head[i]=letter_index[i].begin();
}
bool ans=true;
for(int i=;i<=k-;i++){
bool flag=false;
for(int j=;j<=;j++){
if(used[j]==r[j]) continue;
while(head[j]!=letter_index[j].end() && (*head[j])<=last) head[j]++;
if(head[j]==letter_index[j].end()) continue;
used[j]++;
bool tag=true;
int cnt=,tmp=,pos=(*head[j]);
for(int t=;t<=;t++){
if(suf_sum[pos+][t]+used[t]<l[t]) tag=false;
cnt+=max(,l[t]-used[t]);
tmp+=min(suf_sum[pos+][t],r[t]-used[t]);
}
if(cnt>k--i || tmp<k--i) tag=false;
if(!tag) used[j]--;
else{
res[i]='a'+j;
last=pos;
flag=true;
break;
}
}
if(!flag){
ans=false;
printf("-1\n");
break;
}
}
if(ans){
res[k]='\0';
printf("%s\n",res);
}
}
return ;
}
/*
aaccddaa 6
2 4
0 0
2 2
0 2
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
*/

杭电2019多校第一场,Problem I,String 2019的更多相关文章

  1. 【2019多校第一场补题 / HDU6578】2019多校第一场A题1001Blank——dp

    HDU6578链接 题意 有一串字符串,仅由 {0,1,2,3}\{0, 1, 2, 3\}{0,1,2,3} 组成,长度为 nnn,同时满足 mmm 个条件.每个条件由三个整数组成:l.r.xl.r ...

  2. 2019年杭电多校第一场 1009题String(HDU6586+模拟+单调栈)

    题目链接 传送门 题意 给你一个字符串,要你构造一个长为\(k\)的子串使得每个字母出现的次数在\([L_i,R_i](0\leq i\leq26)\)间且字典序最小. 思路 做这种题目就是要保持思路 ...

  3. 【2019多校第一场补题 / HDU6582】2019多校第一场E题1005Path——最短路径+网络流

    HDU6582链接 题意 在一张有向图中,有一个起点和一个终点,你需要删去部分路径,使得起点到终点的最短距离增加(并不要求需要使得距离变成最大值),且删除的路径长度最短.求删去的路径总长为多少 分析 ...

  4. 2018 Multi-University Training Contest 1 杭电多校第一场

    抱着可能杭电的多校1比牛客的多校1更恐怖的想法 看到三道签到题 幸福的都快哭出来了好吗 1001  Maximum Multiple(hdoj 6298) 链接:http://acm.hdu.edu. ...

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

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

  6. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  7. 牛客多校第一场 B Inergratiion

    牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...

  8. HDU6581 Vacation (HDU2019多校第一场1004)

    HDU6581 Vacation (HDU2019多校第一场1004) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6581 题意: 给你n+1辆汽车, ...

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

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

随机推荐

  1. 栈(Java实现)

    栈是最基本的数据结构之一,其特点是先进后出. 1.基于数组的可动态调节大小的栈 public class ResizingArrayStack<Item> { private Item[] ...

  2. CountDownLatch和CylicBarrier以及Semaphare你使用过吗

    CountDownLatch 是什么 CountDownLatch的字面意思:倒计时 门栓 它的功能是:让一些线程阻塞直到另一些线程完成一系列操作后才唤醒. 它通过调用await方法让线程进入阻塞状态 ...

  3. 【零基础】一文读懂CPU(从二极管到超大规模集成电路)

    一.前言 我们都知道芯片,也知道芯片技术在21世纪是最重要的技术之一,但很少有人能知道芯片技术的一些细节,如芯片是如何构造的.为什么它可以运行程序.芯片又是如何被设计制造出来的等等.本文就尝试从最底层 ...

  4. Navicat 12 for MySQL最新版激活(注册机)(转)(亲测有效)

    Navicat 12 for MySQL最新版激活(注册机)(转)(亲测有效) 一.总结 一句话总结: 1.卸载自己机器上面的Navicat,安装下载的包里面的Navicat安装包,不然可能不行 2. ...

  5. qt QTableView中嵌入复选框CheckBox 的四种方法总结

    第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件 ...

  6. github pr

    github----向开源框架提交pr的过程 https://blog.csdn.net/vim_wj/article/details/78300239github 的 pr 使用方法 https:/ ...

  7. [go]mysql使用

    mysql驱动使用 初始化 import ( "database/sql" _ "github.com/go-sql-driver/mysql" ) DB, e ...

  8. vue 组件属性props,特性驼峰命名,连接线使用

    网址:https://www.cnblogs.com/alasq/p/6363160.html 总结如下:vue的组件的props属性支持驼峰命名,不支持连接线命名,使用是用连接线进行赋值或者数据绑定 ...

  9. ajax基础------备忘

    1:register.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  10. 三分钟读懂TT猫分布式、微服务和集群之路 (转)

    http://www.cnblogs.com/smallSevens/p/7501932.html 针对新手入门的普及,有过大型网站技术架构牛人路过,别耽误浪费了时间,阅读之前,请确保有一定的网络基础 ...