题目链接

传送门

题意

给你一个字符串,要你构造一个长为\(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. http 默认端口

    80是http协议的默认端口,是在输入网站的时候其实浏览器(非IE)已经帮你输入协议了,所以你输入http://baidu.com,其实是访问http://baidu.com:80.而8080,一般用 ...

  2. [技术博客]阿里云签名机制字符串的C语言实现

    [技术博客]阿里云签名机制字符串的C语言实现 问题描述见:阿里云签名机制 话不多说,上字符串函数转化函数代码 bool AlicloudRequest::sendV2Request() { if( q ...

  3. jquery设置bootstrap-select的默认选中值

    <select id="mSelect"></select> $("#mSelect").val(["1",&quo ...

  4. CentOS设置时区

    1.使用date命令查看当前时间 2.已软连接的方式设置时区 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  5. SCIE和SCI

    SCI和SCIE(SCI Expanded)分别是科学引文索引及科学引文索引扩展版(即网络版),主要是收录自然科学.工程技术领域最具影响力的重要期刊,包括2000多种外围刊. SCIE和SCI一样吗? ...

  6. SQLServer ---------- 安装SQLserver数据库

    1.安装SQLserver 数据2008 的地址 https://jingyan.baidu.com/article/948f592434b407d80ef5f97d.html?qq-pf-to=pc ...

  7. spring 事件使用

    1.事件定义 import lombok.Data; import org.springframework.context.ApplicationEvent; /** * 事件定义,这里监听MsgMe ...

  8. FPGA控制RGMII接口PHY芯片基础

    一.前言 网络通信中的PHY芯片接口种类有很多,之前接触过GMII接口的PHY芯片RTL8211EG.但GMII接口数量较多,本文使用RGMII接口的88E1512搭建网络通信系统.这类接口总线位宽小 ...

  9. .Net Core3.0使用gRPC 和IdentityServer4

    gRPC是什么gRPC是可以在任何环境中运行的现代开源高性能RPC框架.它可以通过可插拔的支持来有效地连接数据中心内和跨数据中心的服务,以实现负载平衡,跟踪,运行状况检查和身份验证.它也适用于分布式计 ...

  10. 『保卫王国 树上倍增dp』

    保卫王国 Description Z 国有n座城市,n - 1条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻扎军队.驻扎军队需 ...