题目描述

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. codeforces#1196F. K-th Path(最短路,思维题)

    题目链接: https://codeforces.com/contest/1196/problem/F 题意: 在无向图的所有最短路点对中,求出第$k$大 数据范围: $ 1 \leq k \leq ...

  2. go面试

    **1**.简述golang中make和new的区别 make用于内建类型(只能用于创建map.slice 和channel)的内存分配.并且返回一个有初始值(非零)的T类型,而不是*T. new用于 ...

  3. log4j.properties log4j.xml 路径问题

  4. mysql zip方式安装

    下载zip文件解压到安装目录,此时是没有data文件夹和my.ini文件的. 1.首先自己新建my.ini,内容如下: [client] port=3306 default-character-set ...

  5. js实现简单的文件上传

    文件下载:https://www.cnblogs.com/xiaomili/p/10521160.html  html: <form name="form1" id=&quo ...

  6. C++中.cpp和.hpp的区别

    原文地址:https://blog.csdn.net/qzx9059/article/details/89210571 c++中 cpp和hpp我们可以将所有东西都放在一个.cpp文件内,编译器会将这 ...

  7. easyUI之progressbar进度条

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  8. 如何在 CentOS 里下载 RPM 包及其所有依赖包

    方法一.利用 Downloadonly 插件下载 RPM 软件包及其所有依赖包 默认情况下,这个命令将会下载并把软件包保存到 /var/cache/yum/ 的 rhel-{arch}-channel ...

  9. python matlab 带包实现全排列

    >> A=[2,5,7];perms(A) ans = 7 5 2 7 2 5 5 7 2 5 2 7 2 7 5 2 5 7 >> perms(1:4) ans = 4 3 ...

  10. 改进后的socket轮子,欢迎挑战

    运行环境.net core2.1 下载地址