Codeforces Round #584 - Dasha Code Championship - Elimination Round C. Paint the Digits

题目链接: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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值