码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-06-24 22:08:53      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:题意   pre   string   选择   com   ref   删除   std   using   

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

 

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

标签:题意   pre   string   选择   com   ref   删除   std   using   

原文地址:https://www.cnblogs.com/Kanoon/p/13189197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!