杭电2019多校第一场,Problem I,String 2019
题目描述
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.
输入
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.
输出
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的更多相关文章
- 【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 ...
- 2019年杭电多校第一场 1009题String(HDU6586+模拟+单调栈)
题目链接 传送门 题意 给你一个字符串,要你构造一个长为\(k\)的子串使得每个字母出现的次数在\([L_i,R_i](0\leq i\leq26)\)间且字典序最小. 思路 做这种题目就是要保持思路 ...
- 【2019多校第一场补题 / HDU6582】2019多校第一场E题1005Path——最短路径+网络流
HDU6582链接 题意 在一张有向图中,有一个起点和一个终点,你需要删去部分路径,使得起点到终点的最短距离增加(并不要求需要使得距离变成最大值),且删除的路径长度最短.求删去的路径总长为多少 分析 ...
- 2018 Multi-University Training Contest 1 杭电多校第一场
抱着可能杭电的多校1比牛客的多校1更恐怖的想法 看到三道签到题 幸福的都快哭出来了好吗 1001 Maximum Multiple(hdoj 6298) 链接:http://acm.hdu.edu. ...
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
- 2019年牛客多校第一场B题Integration 数学
2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...
- 牛客多校第一场 B Inergratiion
牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...
- HDU6581 Vacation (HDU2019多校第一场1004)
HDU6581 Vacation (HDU2019多校第一场1004) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6581 题意: 给你n+1辆汽车, ...
- 2019HDU多校第一场1001 BLANK (DP)(HDU6578)
2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...
随机推荐
- PHP 电子围栏算法-不依赖任何第三方接口
<?php /** * @name 围栏算法,判断一个坐标,是否在围栏里面.如:['113.664673,34.810146','113.681667,34.796896','113.69231 ...
- 图论——Floyd算法拓展及其动规本质
一.Floyd算法本质 首先,关于Floyd算法: Floyd-Warshall算法是一种在具有正或负边缘权重(但没有负周期)的加权图中找到最短路径的算法.算法的单个执行将找到所有顶点对之间的最短路径 ...
- 深入理解Java的三大特性之多态
世界上最美丽的东西,看不见也摸不着,要靠心灵去感受. ——海伦·凯勒 面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响类使用的情况下改变类的内部结构,并保护数据.对 ...
- PHP学习之PHP代码的优化
if代码块的优化 if(1===$orderState){ $status='success'; }else{ $status='error'; } return $status; 简 ...
- P2341 [HAOI2006]受欢迎的牛(更完)
P2341 [HAOI2006]受欢迎的牛 题解 tarjan 缩点板子题 如果 A 稀饭 B,那就 A 向 B 连边,构造出一个有向图 如果这个有向图里有强连通分量,也就说明这个强连通分量里的所有奶 ...
- Oracle事务、视图、序列
回顾什么是事务? 一个不可分割的子操作形成一个整体,该整体要么全部执行成功,要么全部执行失败.例如:转帐 回顾为什么要用事务? 如果不用事务的话,为转帐为例,可能出现一个用户钱增加了,另一个用户钱不变 ...
- vector subscript out of range
报这个错时会弹出一个窗口,貌似内存溢出,这是什么由于vector存放的数据超出了vector的大小所造成的. 解决方法如下: 在Vector<string> vector之后,不能直接通过 ...
- centos7最小安装怎么安装防火墙
CentOS 7.0默认使用的是firewall作为防火墙,需要事先关闭. 关闭firewall: 1 2 3 systemctl stop firewalld.service systemctl d ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
笔记 第六章 微服务网关zuul开发实战 1.微服务网关介绍和使用场景 简介:讲解网关的作用和使用场景 (画图) 1)什么是网关 API Gateway,是系 ...
- [CDH] Redis: Remote Dictionary Server
基本概念 一.安装 Redis: Remote Dictionary Server 远程字典服务 使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种 ...