题目网址:https://www.lanqiao.cn/courses/20046/learning/?id=920637
题目描述
给定一个单词,请问在单词中删除 tt 个字母后,能得到的字典序最小的单词是什么?
输入描述
输入的第一行包含一个单词,由大写英文字母组成。
第二行包含一个正整数 tt。
其中,单词长度不超过 100100,tt 小于单词长度。
输出描述
输出一个单词,表示答案。
输入输出样例
示例 1
输入
LANQIAO
3
输出
AIAO
运行限制
- 最大运行时间:1s
- 最大运行内存: 128M
说明:题目只有一类样例,所以很容易过,但是这样子所以无法确定该题题解是否正确,不过大体思路应该就是用栈来模拟字符串的变化,代码如下:
#include<bits/stdc++.h>
using namespace std;
//用栈来模拟吧
stack<char>st;
stack<char>st1;
string s;
int n;
int main(){
cin>>s;
cin>>n;
st.push(s[0]);
for(int i=1;i<s.length();i++){
while(!st.empty()&&s[i]<st.top()&&n!=0){
st.pop();
n--;
}
st.push(s[i]);
}
while(!st.empty()){
st1.push(st.top());
st.pop();
}
while(!st1.empty()){//建一个临时栈,为了正序输出目标字符串
cout<<st1.top();
st1.pop();
}
}