B - 459 Editorial by AwashAmityOak


文字と \(C_i\) の対応関係が、大体 \(3\) 文字ごとになっていることに着目して、以下のようなアルゴリズムが考えられます。

  • \(x\) に、\(S_i\)の先頭の文字とaの文字コードの差分を代入する。
  • もし \(x \geq 18\) なら(s以降の文字なら)、そのときに限り、\(x \leftarrow x-1\) とする。
  • \(x \leftarrow \min(\lfloor \frac{x}3 \rfloor + 2, 9)\) とする。
  • こうして得られた \(x\)\(C_i\) とする。

実装例(C++)

https://atcoder.jp/contests/abc459/submissions/76109017

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;
    
    for (int i = 0; i < N; i++) {
        string Si;
        cin >> Si;
        
        int x = Si[0] - 'a';
        if ('s' - 'a' <= x) x -= 1;
        x = min(x / 3 + 2, 9);
        cout << x;
    }
    cout << endl;
}

実装例(Python)

  • 内包表記を用いて計算/出力部分を1行で書いた例

https://atcoder.jp/contests/abc459/submissions/76109047

N = int(input())
S = input().split()
print(*[min((ord(s[0]) - ord("a") - ("s" <= s[0])) // 3 + 2, 9) for s in S], sep="")

posted:
last update: