公式

B - aaaadaa 解説 by sounansya


AtCoder をはじめたばかりで何をしたらよいか分からない方は、まずは practice contest の問題A「Welcome to AtCoder」を解いてみてください。基本的な入出力の方法が載っています。
また、プログラミングコンテストの問題に慣れていない方は、AtCoder Beginners Selection の問題をいくつか解いてみることをおすすめします。


以下の順に操作を行うと、 \(T\) が求める文字列となります。

  • \(T\) を空文字列とする。
  • \(S\) の各文字を順番に見ていく。
    • 今見ている文字が \(x\) なら \(T\) の末尾に \(x\) を連結する。 \(x\) でないなら \(T\) の末尾に \(y\) を連結する。

この手順をプログラムで実装した例は、以下に記載しています。

実装例 (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)

実装例(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;
}

投稿日時:
最終更新: