CF1009B Minimum Ternary String

解决给定三元字符串通过特定交换规则转化为字典序最小形式的问题,涉及字符串操作和优化技巧。
摘要由CSDN通过智能技术生成

Minimum Ternary String

题目描述

You are given a ternary string (it is a string which consists only of characters ‘0’, ‘1’ and ‘2’).

You can swap any two adjacent (consecutive) characters ‘0’ and ‘1’ (i.e. replace “01” with “10” or vice versa) or any two adjacent (consecutive) characters ‘1’ and ‘2’ (i.e. replace “12” with “21” or vice versa).

For example, for string “010210” we can perform the following moves:

  • “010210” → \rightarrow “100210”;
  • “010210” → \rightarrow “001210”;
  • “010210” → \rightarrow “010120”;
  • “010210” → \rightarrow “010201”.

Note than you cannot swap “02” $ \rightarrow $ “20” and vice versa. You cannot perform any other operations with the given string excluding described above.

You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).

String a a a is lexicographically less than string b b b (if strings a a a and b b b have the same length) if there exists some position i i i ( 1 ≤ i ≤ ∣ a ∣ 1 \le i \le |a| 1ia , where ∣ s ∣ |s| s is the length of the string s s s ) such that for every j < i j < i j<i holds a j = b j a_j = b_j aj=bj , and a i < b i a_i < b_i ai<bi .

输入格式

The first line of the input contains the string s s s consisting only of characters ‘0’, ‘1’ and ‘2’, its length is between 1 1 1 and 1 0 5 10^5 105 (inclusive).

输出格式

Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).

题面翻译

题目描述

给定一个由 ‘0’, ‘1’, ‘2’ 组成的字符串 S S S 。可以交换相邻’0’, ‘1’或’1’, '2’的位置(例如:‘12’ - ‘21’    \; ‘01’ - ‘10’)请输出原字符串经过任意转换后字典序最小的字符串。原字符串长度不超过 1 0 5 10^5 105

输入格式

字符串 S S S

输出格式

转化后字典序最小的字符串

样例 #1

样例输入 #1

100210

样例输出 #1

001120

样例 #2

样例输入 #2

11222121

样例输出 #2

11112222

样例 #3

样例输入 #3

20

样例输出 #3

20

思路

这道题单纯模拟肯定会超时,但我们会发现 0 0 0 1 1 1可以交换, 1 1 1 2 2 2可以交换 0 0 0 2 2 2不可以交换,也就是说 1 1 1可以换到任何位置,因此我们可以先把所有的 1 1 1取出,得到一个由 0 0 0 2 2 2组成的字符串,由于 0 0 0 2 2 2 不能交换,且要得到字典序最小的字符串,我们要把所有的 1 1 1 放在第一个 2 2 2 前。
然后要再特判一下没有 2 的字符串。

代码

/*************************************************
Note:
*************************************************/
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <iomanip>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
inline int read()
{
    int s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}
int n, cnt, num;
string s, ans;
int main()
{
    cin >> s;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == '1')
        {
            cnt++;
            continue;
        }
        ans += s[i];
    }
    num = ans.size();
    for (int i = 0; i < ans.size(); i++)
    {
        if (ans[i] == '2')
        {
            num = i;
            break;
        }
    }
    for (int j = 0; j < cnt; j++)
    {
        ans.insert(ans.begin() + num, '1');
    }
    cout << ans << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

꧁Q༒ོγ꧂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值