Official

A - flip Editorial by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).


This problem can be solved by

  • receiving a string from the input,
  • modifying a character in the string to the corresponding one, and
  • printing the modified string.

If you are only interested in the sample code, see the sample code below in C++ and Python. Below the sample codes is a note about the difference in the implementation due to different language features.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cin >> s;
    
    for (char c : s)
        if (c == '0')
            cout << '1';
        else
            cout << '0';

    cout << endl;

    return 0;
}
s = input()
print(s.replace('0', 'x').replace('1', '0').replace('x', '1'))

Depending on the language you use, the easiness of the implementation varies.

“Modifying a character in the string to the corresponding one” can be done as follows:

  • Some languages have a feature to replace all occurrences of a character in a string to another.
    • e.g. Python
  • Some languages are even capable of doing the operation above for multiple pairs of characters at once.
    • e.g. Bash and Ruby
  • Some languages do not have such a feature (or even if it does, it requires an advanced concept) so you might as well implement by yourself.
    • e.g. C++ and Haskell

Also, “receiving a string from the input” and “printing the modified string” can be done as follows:

  • Some languages provides a way to directly print the string obtained by modifying the input.
    • e.g. Bash and Haskell
  • Some languages enables us to receive a string into a variable and print a string out of a variable.
    • e.g. Ruby, Python, and C++
  • In some languages, you just have to receive the string one character by one.
    • We don’t explain it, but Brainfuck is an example.

We take Bash, Haskell, Ruby, Python, and C++ as examples, together with sample codes.

Bash

The tr command in Bash is the go-to command when it comes to replacing the characters in the input with others.
With tr ab cd, the as in the input are replaced with cs, and bs with ds.

A sample code follows.

tr 01 10

Haskell

Haskell has a function called “interact”, which receives a function that converts a string to another and print the result.

To replace 0 with 1 and 1 with 0, (probably) writing by yourself is the simplest.

main = interact $ map (
    \c -> if c == '0' then
              '1'
          else if c == '1' then
              '0'
          else
              c
    )

Ruby

A Ruby string has a method called “tr”. With s.tr("ab", "cd"), the as in the input are replaced with cs, and bs with ds.

You can use gets to receive the input, tr to replace the characters, and puts to print the result.

puts gets.tr("01", "10")

Python

A Python string has a method called “replace”. With s.replace('a', 'b'), the as in the input are replaced with b. In case of swapping two characters,

s.replace('0', '1').replace('1', '0') # Not working

does not work because the first replace('0', '1') makes all characters 1. That’s why need to replace 0 with a irrelevant character at first:

s.replace('0', 'x').replace('1', '0').replace('x', '1') # Working

You can use input to receive the input, replace to replace the characters, and print to print the result.

print(input().replace('0', 'x').replace('1', '0').replace('x', '1'))

Also, a Python string has a method called translate, which receives an appropriate “map” and replaces each character with the given ones.

print(input().translate({ord('0'): '1', ord('1'): '0'}))

The map provided for translate shall be constructed with the str.maketrans function. You may write

print(input().translate(str.maketrans({'0': '1', '1': '0'})))

or

print(input().translate(str.maketrans('01', '10')))

C++

In C++, you may receive the input into a string-type variable, replace the characters one by one, and print the result.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cin >> s;

    for (char c : s)
        if (c == '0')
            cout << '1';
        else
            cout << '0';

    cout << endl;

    return 0;
}

Alternatively, you may edit \(s\) and finally print the result.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cin >> s;

    for (char& c : s)
        if (c == '0')
            c = '1';
        else
            c = '0';

    cout << s << endl;

    return 0;
}

C++ has a function called “replace”, which replaces the values within a range to another. You may use the “replace” function as follows. Just as in Python, note that you need a bypass with an irrelevant character.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    string s;
    cin >> s;

    replace(s.begin(), s.end(), '0', 'x');
    replace(s.begin(), s.end(), '1', '0');
    replace(s.begin(), s.end(), 'x', '1');

    cout << s << endl;

    return 0;
}

posted:
last update: