-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_Decreasing_String.cpp
75 lines (65 loc) · 1.46 KB
/
C_Decreasing_String.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
67
68
69
70
71
72
73
#include<bits/stdc++.h>
# define MULTI
//# define INIT
using namespace std;
#include<math.h>
# define endl '\n'
# define space ' '
# define LL long long
# define PII pair<int, int>
// by hangpengjie
/**
* 题目: C. Decreasing String
* 链接: https://codeforces.com/contest/1886/problem/C
* 省流: 双链表非常规解法,正解是单调栈!!! 大佬的代码: https://codeforces.com/contest/1886/submission/227350420
*
* 双链表解法: 记录每个字母s[i]对应的l[i] 和 r[i] 如果s[i] > s[r[i]] 说明在这一轮中可以将s[i]删掉同时记得更新l[r[i]] 以及 r[l[i]]
* 代码: https://codeforces.com/contest/1886/submission/228567235
*/
void slove(){
string s;
LL pos, n;
cin >> s >> pos;
n = s.size();
s = '0' + s;
vector<int> l(n+1, 0),r(n+1, 0);
for(int i=1; i<=n; ++i){
l[i] = i-1;
r[i] = i+1;
}
r[0] = 1;
r[n] = 0;
int d = n;
int i = 1;
while(pos > d){
while(s[i] <= s[r[i]]) i = r[i]; // 这里一定不能写成 i++ 把i想象成链表处理!!!
r[l[i]] = r[i];
l[r[i]] = l[i];
i = l[i];
pos -= d;
d--;
}
int c = 0;
while(pos > 0){
c = r[c];
pos--;
}
cout << s[c] ;
}
# ifdef INIT
void init(){
}
# endif
int main()
{
# ifdef INIT
init();
# endif
int cas = 1;
# ifdef MULTI
cin >> cas;
# endif
while (cas--){
slove();
}
}