题目链接:https://codeforces.com/contest/1209/problem/C
思路:先在字符串中选出一个满足最大数字小于等于未选中数字的非递减子序列(我当时用的单调栈。。),然后判断另一个序列是不是非递减即可。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int vis[maxn];
int main() {
int t;
ios::sync_with_stdio(0);
cin >> t;
while(t--) {
int n;
cin >> n;
string s;
cin >> s;
stack <int> st;
int ct = 0x7f7f7f7f;
memset(vis,0,sizeof(vis));
for(int i = 0 ; i < n ; i++) {
while(!st.empty() && s[st.top()] > s[i]) {
ct = min(ct,(int)s[st.top()]);
st.pop();
}
if(s[i] <= ct)
st.push(i);
}
string t = "";
char cnt = 0;
if(!st.empty())cnt = s[st.top()];
while(!st.empty()) {
vis[st.top()] = 1; st.pop();
}
for(int i = 0 ; i < n ; i++) {
if(!vis[i]) {
t += s[i];
vis[i] = 2;
}
}
bool flag = 0;
for(int i = 1 ; i < t.length() ; i++) {
if(t[i] < t[i - 1]) {
flag = 1;break;
}
}
if(!flag) {
if((t.length() && cnt <= t[0])|| !t.length()) {
for(int i = 0 ; i < n ; i++) {
cout << vis[i];
}
cout << "\n";
}
else cout << "-\n";
}
else cout << "-\n";
}
return 0;
}