-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTACK015 - Loại K số.cpp
66 lines (48 loc) · 1.25 KB
/
STACK015 - Loại K số.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
string removeKdigits(string num, int k) {
if(num.length() <= k)
return "0";
if(k == 0)
return num;
string res = "";
stack <char> s;
s.push(num[0]);
for(int i = 1; i<num.length(); ++i)
{
while(k > 0 && !s.empty() && num[i] < s.top())
{
--k;
s.pop();
}
s.push(num[i]);
if(s.size() == 1 && num[i] == '0')
s.pop();
}
while(k && !s.empty())
{
--k;
s.pop();
}
while(!s.empty())
{
res.push_back(s.top());
s.pop();
}
reverse(res.begin(),res.end());
if(res.length() == 0)
return "0";
return res;
}
int main(){
int t;
cin>>t;
cin.ignore();
while(t--){
string s;
int k;
cin>>s>>k;
cout<<removeKdigits(s,k)<<"\n";
}
// system("pause");
}