Official

A - o-padding Editorial by yuto1115


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


基本的には問題文で指示されていることをそのまま実装すればよいですが、その実装の方針としては以下の二通りが考えられます。

  1. For 文等のループを用いて、o\(N-|S|\) 回出力したのち、\(S\) を出力する。
  2. o\(N-|S|\) 個繋げてできる文字列 \(T\) を作り、\(T\)\(S\) をこの順に出力する。

2 の方針は少々回りくどく感じるかもしれませんが、C++, Python などの言語では「ある文字を特定の回数繰り返してできる文字列」を作るための構文が用意されているため、より簡潔に書けることがあります。詳細は下記の実装例 (C++, Python) を参照してください。

方針 1 の実装例 (C++) :

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    string s;
    cin >> n >> s;
    for (int i = 0; i < n - s.size(); i++) {
        cout << 'o';
    }
    cout << s << endl;
}

方針 1 の実装例 (Python) :

n = int(input())
s = input()
for i in range(n - len(s)):
    print('o', end='')
print(s)

方針 2 の実装例 (C++) :

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    string s;
    cin >> n >> s;
    string t(n - s.size(), 'o');
    cout << t << s << endl;
}

方針 2 の実装例 (Python) :

n = int(input())
s = input()
t = 'o' * (n - len(s))
print(t + s)

posted:
last update: