题目链接

传送门

题意

给你一个字符串,要你构造一个长为\(k\)的子串使得每个字母出现的次数在\([L_i,R_i](0\leq i\leq26)\)间且字典序最小。

思路

做这种题目就是要保持思路清晰,博主就是因为写的时候没有想清楚写了一晚上\(+\)一个早上……

首先我们对于第\(i\)个位置有如果这个位置可以摆放,那么\(L[s[i]-'a'],R[s[i]-'a'],k\)均减少\(1\),如果不能摆放(条件为:\(\sum\limits_{i=0}^{26}L[i]\leq tot-1,L[i]>0\))那么就\(continue\)。

至于字典序最小,我们对于每个可以摆放的位置用单调栈来维护即可。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 100000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int tot;
char s[maxn];
stack<int> st;
vector<char> vec;
int L[30], R[30], sum[maxn][30]; int main() {
while(~scanf("%s%d", s, &tot)) {
int n = strlen(s);
for(int i = 0; i < 26; ++i) sum[n][i] = 0;
for(int i = 0; i < 26; ++i) {
scanf("%d%d", &L[i], &R[i]);
}
for(int i = n - 1; i >= 0; --i) {
for(int j = 0; j < 26; ++j) {
sum[i][j] = sum[i+1][j];
}
int x = s[i] - 'a';
++sum[i][x];
}
while(!st.empty()) st.pop();
for(int i = 0; i < n; ++i) {
int x = s[i] - 'a', cnt = 0;
if(R[x] == 0) continue;
--L[x], --R[x];
for(int j = 0; j < 26; ++j) {
if(L[j] > 0) cnt += L[j];
}
if(tot > 0 && cnt > tot - 1) {
++L[x], ++R[x];
continue;
}
if(tot > 0) {
while(!st.empty() && s[i] < s[st.top()]) {
int tmp = s[st.top()] - 'a';
if(L[tmp] + 1 > sum[i+1][tmp]) break;
++L[tmp], ++R[tmp];
st.pop();
++tot;
}
--tot;
st.push(i);
} else if(tot == 0) {
while(!st.empty() && s[i] < s[st.top()]) {
int tmp = s[st.top()] - 'a';
if(L[tmp] + 1 > 0) break;
++L[tmp], ++R[tmp];
st.pop();
++tot;
}
if(tot > 0) {
--tot;
st.push(i);
} else {
++L[x], ++R[x];
}
} else {
++L[x], ++R[x];
}
}
int flag = 1;
for(int i = 0; i < 26; ++i) {
if(L[i] > 0) {
flag = 0;
break;
}
}
if(!flag || tot > 0) {
printf("-1\n");
continue;
}
vec.clear();
while(!st.empty()) {
vec.push_back(s[st.top()]);
st.pop();
}
for(int i = (int)vec.size() - 1; i >= 0; --i) {
printf("%c", vec[i]);
}
printf("\n");
}
return 0;
}

2019年杭电多校第一场 1009题String(HDU6586+模拟+单调栈)的更多相关文章

  1. 2019年杭电多校第一场 1004题Vacation(HDU6581+数学)

    题目链接 传送门 题意 有\(n+1\)辆车要过红绿灯,告诉你车的长度.与红绿灯的起点(题目假设红绿灯始终为绿).车的最大速度,问你第\(0\)辆车(距离最远)车头到达红绿灯起点的时间是多少(每辆车最 ...

  2. 2019年杭电多校第一场 1002题Operation(HDU6579+线性基)

    题目链接 传送门 题意 初始时有\(n\)个数,现在有\(q\)次操作: 查询\([l,r]\)内选择一些数使得异或和最大: 在末尾加入一个数. 题目强制在线. 思路 对于\(i\)我们记录\([1, ...

  3. 2019年杭电多校第二场 1002题Beauty Of Unimodal Sequence(LIS+单调栈)

    题目链接 传送门 思路 首先我们对\(a\)正反各跑一边\(LIS\),记录每个位置在前一半的\(LIS\)中应该放的位置\(ans1[i]\),后一半的位置\(ans2[i]\). 对于字典序最小的 ...

  4. Rikka with Travels(2019年杭电多校第九场07题+HDU6686+树形dp)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 定义\(L(a,b)\)为结点\(a\)到结点\(b\)的路径上的结点数,问有种\(pair(L(a,b),L(c,d))\)取值,其中结点\ ...

  5. 2019年杭电多校第二场 1008题Harmonious Army(HDU6598+最小割+建图)

    题目链接 传送门 题意 有\(n\)个士兵,要你给他们分配职业.有\(m\)对关系,对于某一对关系\(u,v\),如果同为勇士则总能力增加\(a\),同法师则增加\(c\),一个勇士一个法师增加\(\ ...

  6. 2019年杭电多校第二场 1012题Longest Subarray(HDU6602+线段树)

    题目链接 传送门 题意 要你找一个最长的区间使得区间内每一个数出现次数都大于等于\(K\). 思路 我们通过固定右端点考虑每个左端点的情况. 首先对于每个位置,我们用线段树来维护它作为\(C\)种元素 ...

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

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

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

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

  9. 2019杭电多校第一场hdu6581 Vacation

    Vacation 题目传送门 update(O(n)) 看了那个O(n)的方法,感觉自己想的那个O(nlogn)的好傻,awsl. 0车最终通过停车线的时候,状态一定是某个车堵住后面的所有车(这个车也 ...

随机推荐

  1. Leetcode 1254. 统计封闭岛屿的数目

    题目: 有一个二维矩阵 grid ,每个位置要么是陆地(记号为 0 )要么是水域(记号为 1 ). 我们从一块陆地出发,每次可以往上下左右 4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座 ...

  2. kali渗透

    局域网-断网&劫持(kali)   1.查看局域网中的主机 fping –asg 192.168.1.0/24 2.断网 arpspoof -i wlan0 -t 192.168.100 19 ...

  3. AVLMap平衡二叉树

    public class AVLMap<K, V> implements Iterable<AVLEntry<K, V>> { private int size; ...

  4. Android studio中遇到的问题

    首先声明只是Android studio使用中遇到的问题纯属个人学习笔记,有什么不对的可以留言. 将脱壳后的java文件拖入到Android studio android studio 首先提示是ER ...

  5. 内网服务器离线编译安装mysql5.7并调优

    目录 内网服务器离线编译安装mysql5.7并调优 前言 关于MySQL 一.MySQL安装篇 部署环境 前期准备工具 挂载系统ISO镜像,配置yum源 二.MySQL调优篇 1.对MySQL进行安全 ...

  6. Topshelf 搭建 Windows 服务

    Topshelf 是一个用来部署基于.NET Framework 开发的服务的框架.简化服务创建于部署过程,并且支持控制台应用程序部署为服务.本文基于 .net core 控制台应用程序部署为服务(. ...

  7. Oulipo 子串查找

    题目描述 思路 使用哈希值表示较长串的子串的值,直接比较哈希值是否相等 代码 #include <cstdio> #include <cstring> using namesp ...

  8. Replication:The transaction log for database 'tempdb' is full due to 'ACTIVE_TRANSACTION'

    今天早上,Dev跟我说,执行query statement时出现一个error,detail info是: “The transaction log for database 'tempdb' is ...

  9. mingw 编译 glfw3 的 helloworld

    glfw3 为基础开发 GUI 似乎是一个不错选项,有很多人尝试这么做了.今天也小试一把. 工具: mingw(不是 mingw-w64),头文件 GLFW/ ,库文件 glfw3.dll 需要注意, ...

  10. vs2017专业版和企业版的密钥

    Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH