提出 #67004781


ソースコード 拡げる

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

typedef long long ll;
typedef pair<int, int> pii;
constexpr int MAXN = 1'000'007;
constexpr int INF = 2'000'000'000;
constexpr ll INFF = 1'000'000'000'000'000'000LL;
constexpr ll MOD = 998'244'353;
#define mkp make_pair
#define F first
#define S second
#define pb emplace_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()

ll ppow(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    y >>= 1;
  }
  return ret;
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  while (t--) {
    constexpr int C = 543210;
    vector<int> lc(C, -1), rc(C, -1);
    vector<ll> dp(C), nodes(C), tag(C);

    string s;
    int nn = 1;
    function<void(int, int)> ins = [&](int cur_node, int idx) {
      if (idx < sz(s)) {
        if (s[idx] == 'A') {
          if (lc[cur_node] == -1) {
            lc[cur_node] = nn++;
          }
          ins(lc[cur_node], idx + 1);
        } else {
          if (rc[cur_node] == -1) {
            rc[cur_node] = nn++;
          }
          ins(rc[cur_node], idx + 1);
        }
      } else {
        tag[cur_node] = 1;
      }
      nodes[cur_node] = tag[cur_node];
      if (lc[cur_node] != -1)
        nodes[cur_node] += nodes[lc[cur_node]];
      if (rc[cur_node] != -1)
        nodes[cur_node] += nodes[rc[cur_node]];

    //   cout << cur_node << ' ' << lc[cur_node] << ' ' << rc[cur_node] << '\n';
      if (tag[cur_node] == 1) {
        dp[cur_node] = ppow(2, nodes[cur_node] - 1);
        if (lc[cur_node] != -1 && rc[cur_node] != -1)
          dp[cur_node] += dp[lc[cur_node]] * dp[rc[cur_node]];
        dp[cur_node] %= MOD;
      } else {
        dp[cur_node] = 0;
        if (lc[cur_node] != -1 && rc[cur_node] != -1)
          dp[cur_node] += dp[lc[cur_node]] * dp[rc[cur_node]];
        dp[cur_node] %= MOD;
      }
    };

    int n;
    cin >> n;
    while (n--) {
      cin >> s;
      ins(0, 0);
      cout << dp[0] << '\n';
    }
  }
}
/*
- How to come up with solutions? (https://hackmd.io/-3cIVAFSQMC3iJTh9EpszA)
  - Play with some small examples.
  - Make observations (or random guesses) by intuition.
    - Try to find counterexamples of the "observations."
  - Find the characteristics of an optimal solution.
  - Try to solve simple cases.
  - Brute force and print out the results.
  - Pick a method (greedy, dp, d&c, ...)
    - But DO NOT force algos on problems!
  - IF YOU'RE STUCK, TRY SOMETHING ELSE! Make new observations!

- Before writing the solution:
  - check TL/ML/correctness of samples/implementation details!

- Pre-submit:
  - Did you make a typo when copying a template?
  - Test more cases if unsure.
    - Write a naive solution and check small cases.
  - Submit the correct file.

- Debugging:
  - General Debugging:
    - Read the whole problem again.
    - Think over the algorithm again.
    - Go to the toilet.

  - Wrong Answer:
    - Any possible overflows?
      - > `__int128` ?
      - Try `-ftrapv` or `#pragma GCC optimize("trapv")`
    - Floating point errors?
      - > `long double` ?
      - turn off math optimizations
      - check for `==`, `>=`, `acos(1.000000001)`, etc.
    - Did you forget to sort or unique?
    - Generate large and worst "corner" cases.
    - Check your `m` / `n`, `i` / `j`,  `x` / `y` and `<` / `>`.
    - Are everything initialized or reset properly?
    - Are you sure about the STL thing you are using?
      - Read cppreference.
    - Print everything and run it on pen and paper.

  - Time Limit Exceeded:
    - Calculate your time complexity again.
    - Does the program actually end?
      - Check for `while(q.size())` etc.
    - Test the largest cases locally.
    - Did you do unnecessary stuff?
      - e.g. pass vectors by value
      - e.g. `memset` for every test case
    - Is your constant factor reasonable?

  - Runtime Error:
    - Check memory usage.
      - Forget to clear or destroy stuff?
      - > `vector::shrink_to_fit()`
    - Stack overflow?
    - Bad pointer / array access?
      - Try `-fsanitize=address`
    - Division by zero? NaN's?
*/

提出情報

提出日時
問題 C - Prefix Covering
ユーザ henotrix
言語 C++ 20 (gcc 12.2)
得点 600
コード長 4280 Byte
結果 AC
実行時間 38 ms
メモリ 51984 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 2
AC × 54
セット名 テストケース
Sample sample-01.txt, sample-02.txt
All 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, 03-01.txt, 03-02.txt, 03-03.txt, 03-04.txt, 03-05.txt, 04-01.txt, 04-02.txt, 04-03.txt, 04-04.txt, 05-01.txt, 05-02.txt, 05-03.txt, 05-04.txt, 05-05.txt, 05-06.txt, 05-07.txt, 05-08.txt, 06-01.txt, 06-02.txt, 06-03.txt, 06-04.txt, 06-05.txt, 06-06.txt, 06-07.txt, 06-08.txt, 07-01.txt, 07-02.txt, 07-03.txt, 07-04.txt, 07-05.txt, 08-01.txt, 08-02.txt, 08-03.txt, 08-04.txt, 09-01.txt, 09-02.txt, 09-03.txt, 09-04.txt, sample-01.txt, sample-02.txt
ケース名 結果 実行時間 メモリ
02-01.txt AC 7 ms 19876 KiB
02-02.txt AC 7 ms 19868 KiB
02-03.txt AC 7 ms 19972 KiB
02-04.txt AC 7 ms 19976 KiB
02-05.txt AC 7 ms 19876 KiB
02-06.txt AC 7 ms 19900 KiB
02-07.txt AC 7 ms 19812 KiB
02-08.txt AC 7 ms 19800 KiB
02-09.txt AC 7 ms 19900 KiB
02-10.txt AC 8 ms 19900 KiB
02-11.txt AC 9 ms 19892 KiB
02-12.txt AC 11 ms 19892 KiB
02-13.txt AC 16 ms 19876 KiB
02-14.txt AC 26 ms 19820 KiB
03-01.txt AC 8 ms 19804 KiB
03-02.txt AC 9 ms 19904 KiB
03-03.txt AC 11 ms 19944 KiB
03-04.txt AC 17 ms 19904 KiB
03-05.txt AC 28 ms 19820 KiB
04-01.txt AC 30 ms 51952 KiB
04-02.txt AC 31 ms 51984 KiB
04-03.txt AC 21 ms 26552 KiB
04-04.txt AC 20 ms 26536 KiB
05-01.txt AC 38 ms 19892 KiB
05-02.txt AC 38 ms 19876 KiB
05-03.txt AC 37 ms 19896 KiB
05-04.txt AC 37 ms 19896 KiB
05-05.txt AC 36 ms 19972 KiB
05-06.txt AC 26 ms 19868 KiB
05-07.txt AC 21 ms 19968 KiB
05-08.txt AC 18 ms 20212 KiB
06-01.txt AC 33 ms 19868 KiB
06-02.txt AC 32 ms 19904 KiB
06-03.txt AC 32 ms 19900 KiB
06-04.txt AC 31 ms 19900 KiB
06-05.txt AC 31 ms 19868 KiB
06-06.txt AC 24 ms 19876 KiB
06-07.txt AC 21 ms 19888 KiB
06-08.txt AC 18 ms 20284 KiB
07-01.txt AC 18 ms 19884 KiB
07-02.txt AC 19 ms 19904 KiB
07-03.txt AC 18 ms 19816 KiB
07-04.txt AC 18 ms 19904 KiB
07-05.txt AC 18 ms 19780 KiB
08-01.txt AC 19 ms 19908 KiB
08-02.txt AC 19 ms 19952 KiB
08-03.txt AC 20 ms 19972 KiB
08-04.txt AC 20 ms 19964 KiB
09-01.txt AC 19 ms 19820 KiB
09-02.txt AC 19 ms 19972 KiB
09-03.txt AC 20 ms 19820 KiB
09-04.txt AC 20 ms 19968 KiB
sample-01.txt AC 7 ms 19908 KiB
sample-02.txt AC 7 ms 19836 KiB