Codeforces Round #652 (Div. 2) B. AccurateLee(字符串)

题目链接:https://codeforces.com/contest/1369/problem/B

题意

给出一个长 $n$ 的 二进制串,每次可以选择字符串中的一个 $10$,然后删除其中的一个字符,问字符串最短及最小的字典序是多少。 

题解

$1 \dots \dots 0$ 最后可以变为 $0$,找到最左边的 $1$ 的位置 $l$ 和最右边的 $0$ 的位置 $r$,若 $l > r$,则说明字符串为 $0 + 1$ 的形式,输出原串即可,否则输出最左边的 $1$ 之前、最右边的 $0$ 及其之后的子串。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n; cin >> n;
    string s; cin >> s;
    int l = n, r = -1;
    for (int i = 0; i < n; i++) {
        if (s[i] == '1') l = min(l, i);
        if (s[i] == '0') r = max(r, i);
    }
    if (l < r)
        cout << s.substr(0, l) + s.substr(r) << "\n";
    else
        cout << s << "\n";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

猜你喜欢

转载自www.cnblogs.com/Kanoon/p/13189197.html
今日推荐