公式

B - aaaadaa 解説 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).


The string \(T\) in the following algorithm will end up in the sought string.

  • Let \(T\) be an empty string.
  • Inspect each character of \(S\).
    • If the currently inspected character is \(x\), append \(x\) to the tail of \(x\); otherwise, append \(y\) to the end of \(T\).

Sample implementation of this algorithm in programming languages are introduced below.

Sample code (Python3)

n, x, y = map(str, input().split())
n = int(n)
s = input()
t = ""
for i in range(n):
    if s[i] == x:
        t += x
    else:
        t += y
print(t)

Sample code (C++)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    char x, y;
    string s;
    cin >> n >> x >> y >> s;
    string t = "";
    for (char c : s)
    {
        t += c == x ? x : y;
    }
    cout << t << endl;
}

投稿日時:
最終更新: