[CF1009B]Minimum Ternary String(思维)

题目链接

http://codeforces.com/problemset/problem/1009/B

题意

给一个只含0、1、2串,只能做相邻的01交换或者相邻的12交换操作,问经过数次操作(可以是0次)能得到的最小字典序的串是什么。

题解

思维题
关键是把题意理解为:固定0、2的相对位置,往里随意放1,能得到的最小字典序。
显然,目标是把所有1放到0后1前好,故放到第一个2222前,若没有2则放到字符串最前。
关于存储串的形式,存到数组记1的数目即可,然后打印做处理即可。

代码

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner in=new Scanner(System.in);
        String str=in.next();
        int cnt=0;
        for(int i=0;i<str.length();++i) {
            if(str.charAt(i)=='1') {
                ++cnt;
            }
        }
        int i=0;
        for(;i<str.length();++i) {
            if(str.charAt(i)=='0') {
                System.out.print("0");
            }
            else if(str.charAt(i)=='2') {
                while(cnt-->0) {//
                    System.out.print("1");
                }
                System.out.print("2");
            }
        }
        while(cnt-->0) {
            System.out.print("1");
        }
        in.close();
    }
}

原文地址:https://www.cnblogs.com/coding-gaga/p/10987337.html

时间: 2024-08-29 05:36:09

[CF1009B]Minimum Ternary String(思维)的相关文章

Balanced Ternary String CodeForces - 1102D (贪心+思维)

You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings. Your task is to replace minimum number of characters in this string with other characters to obtain

Minimum Sum(思维)

Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB    Total Submit: 563  Accepted: 156  Special Judge: No Description There are n numbers A[1] , A[2] .... A[n], you can select m numbers of it A[B[1]] , A[B[2]] ... A[B[m]]  ( 1 <= B

Educational Codeforces Round 47(Div 2)(A~D)

比赛链接 A.Game Shopping 这种题真是..打开网页1min 读题3min 思考0min 写代码0.5min.. #include <cstdio> #include <cctype> #include <algorithm> #define gc() getchar() const int N=1e3+5; int n,m,cost[N],A[N]; inline int read() { register int now=0;register char

字符串String

字符串String思维导图 创建字符串 String类即字符串类型,像基本数据类型一样使用,用双引号括起来进行声明.在Java中用String类的构造方法来创建字符串变量. 1.String() 一个String对象,可表示一个空字符序列. 例 :使用String()方法初始化一个新创建的String对象. String s=new String(); 2.String(char a[]) 用一个字符数组a创建String对象. 例 :使用String(char a[])方法创建String对象

Codeforces Round #598 (Div. 3) D - Binary String Minimizing

原文链接:https://www.cnblogs.com/xwl3109377858/p/11797618.html Codeforces Round #598 (Div. 3) D - Binary String Minimizing You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1'). In one move you can swap two adj

hack with python(二)

环境:dvwa 1.7数据库: mysql前置知识:  1.阅读了关于sql注入基础的两个博文并自己动手实践过(一)(二)    2.阅读了hack with python(一) 一.学习web安全的过程并不总是充满快乐,有时还有点小枯燥(1)那这样我们,先来玩个小游戏吧!猜数字,看一看你能多少次猜出数字 #! /usr/bin/python #A game to a guess a number between 1 and 100 #if the times you guess is less

winform开线程,避免页面假死

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Text.RegularExpressions;

LUXURY 8

A - Gargari and Bishops Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 463C Description Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prov

Codeforces Round #265 (Div. 2) C. No to Palindromes!

Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more. Paul has found a tolerable string