公式
B - Center Alignment 解説 by en_translator
Let \(|S|\) denote the length of \(S\). Then the value \(k\) in the condition of \(T_i\) is \(k=\dfrac{m-|S_i|}{2}\).
Possible ways to find \(T_i\) include “prepending and appending a . to the head and tail of \(S_i\), \(k\) times,” and “use the \(k\)-time repetition of ..”
Sample code (C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<string> S(N);
for (int i = 0; i < N; i++) cin >> S[i];
int m = 0;
for (int i = 0; i < N; i++) m = max(m, (int)S[i].size());
for (int i = 0; i < N; i++) {
int k = (m - S[i].size()) / 2;
string T = string(k, '.') + S[i] + string(k, '.');
cout << T << "\n";
}
}
Sample code (Python)
N = int(input())
S = [input() for _ in range(N)]
m = max(len(s) for s in S)
for s in S:
k = (m - len(s)) // 2
t = ("." * k) + s + ("." * k)
print(t)
投稿日時:
最終更新: