結果

問題 No.1481 Rotation ABC
ユーザー MisterMister
提出日時 2021-04-19 19:41:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 999 bytes
コンパイル時間 1,011 ms
コンパイル使用メモリ 75,072 KB
最終ジャッジ日時 2025-01-20 22:09:16
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/modint>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
using mint = atcoder::modint998244353;

mint fact(int n) {
    mint ret = 1;
    for (int i = 1; i <= n; ++i) ret *= i;
    return ret;
}

// sはtを部分列として含むか
bool contain(const string& s, const string& t) {
    int m = t.length();
    int i = 0;
    for (auto c : s) {
        if (i < m && c == t[i]) ++i;
    }
    return i == m;
}

void solve() {
    int n;
    string s;
    cin >> n >> s;

    if (contain(s, "ABC") ||
        contain(s, "BCA") ||
        contain(s, "CAB")) {
        auto ans = fact(n);
        for (char c : "ABC") {
            // null文字も入るが、0個なので気にしない
            ans /= fact(count(s.begin(), s.end(), c));
        }
        ans -= n;

        cout << ans.val() << "\n";
    } else {
        cout << "1\n";
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0