Submission #65267074


Source Code Expand

#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()

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;

  int rt = 1, ans = 0;
  vector<vector<int>> trie(MAXN, vector<int>(26));
  vector<int> tag(MAXN), cnt(MAXN);

  auto insert1 = [&](string &s) {
    int now = 1;
    bool ok = 1;
    for (char c : s) {
      if (trie[now][c - 'a'] == 0) {
        trie[now][c - 'a'] = ++rt;
      }
      now = trie[now][c - 'a'];

      if (tag[now]) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      tag[now] = 1;

      queue<int> q;
      q.push(now);
      while (!q.empty()) {
        auto nt = q.front();
        q.pop();
        ans -= cnt[nt];
        for (int i = 0; i < 26; i++) {
          if (trie[nt][i] && !tag[trie[nt][i]]) {
            q.push(trie[nt][i]);
          }
        }
      }
    }
  };

  auto insert2 = [&](string &s) {
    int now = 1;
    bool ok = 1;
    for (char c : s) {
      if (trie[now][c - 'a'] == 0) {
        trie[now][c - 'a'] = ++rt;
      }
      now = trie[now][c - 'a'];
      if (tag[now]) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      cnt[now]++;
      ans++;
    }
  };

  for (int i = 0; i < q; i++) {
    int t;
    string s;
    cin >> t >> s;
    if (t == 1) {
      insert1(s);
    } else {
      insert2(s);
    }
    cout << ans << '\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?
*/

Submission Info

Submission Time
Task E - Forbidden Prefix
User henotrix
Language C++ 20 (gcc 12.2)
Score 500
Code Size 3956 Byte
Status AC
Exec Time 101 ms
Memory 144364 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 57
Set Name Test Cases
Sample 00_sample_01.txt, 00_sample_02.txt
All 00_sample_01.txt, 00_sample_02.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt, 02_handmade_10.txt, 02_handmade_11.txt, 02_handmade_12.txt, 02_handmade_13.txt, 02_handmade_14.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 73 ms 143716 KiB
00_sample_02.txt AC 74 ms 143712 KiB
01_random_01.txt AC 78 ms 143908 KiB
01_random_02.txt AC 79 ms 143888 KiB
01_random_03.txt AC 79 ms 144036 KiB
01_random_04.txt AC 79 ms 143880 KiB
01_random_05.txt AC 79 ms 143948 KiB
01_random_06.txt AC 79 ms 144004 KiB
01_random_07.txt AC 79 ms 143844 KiB
01_random_08.txt AC 79 ms 143728 KiB
01_random_09.txt AC 87 ms 143732 KiB
01_random_10.txt AC 85 ms 143688 KiB
01_random_11.txt AC 82 ms 143732 KiB
01_random_12.txt AC 84 ms 143792 KiB
01_random_13.txt AC 79 ms 143752 KiB
01_random_14.txt AC 98 ms 143748 KiB
01_random_15.txt AC 97 ms 143716 KiB
01_random_16.txt AC 97 ms 143752 KiB
01_random_17.txt AC 86 ms 143708 KiB
01_random_18.txt AC 96 ms 143748 KiB
01_random_19.txt AC 79 ms 144084 KiB
01_random_20.txt AC 74 ms 144112 KiB
01_random_21.txt AC 77 ms 143760 KiB
01_random_22.txt AC 81 ms 143752 KiB
01_random_23.txt AC 78 ms 143728 KiB
01_random_24.txt AC 96 ms 143760 KiB
01_random_25.txt AC 92 ms 143748 KiB
01_random_26.txt AC 90 ms 143732 KiB
01_random_27.txt AC 87 ms 143792 KiB
01_random_28.txt AC 95 ms 143740 KiB
01_random_29.txt AC 87 ms 143728 KiB
01_random_30.txt AC 91 ms 143704 KiB
01_random_31.txt AC 85 ms 143616 KiB
01_random_32.txt AC 88 ms 143760 KiB
01_random_33.txt AC 79 ms 143792 KiB
01_random_34.txt AC 94 ms 143748 KiB
01_random_35.txt AC 88 ms 143684 KiB
01_random_36.txt AC 89 ms 143740 KiB
01_random_37.txt AC 95 ms 143724 KiB
01_random_38.txt AC 101 ms 143712 KiB
01_random_39.txt AC 96 ms 143708 KiB
01_random_40.txt AC 95 ms 143652 KiB
01_random_41.txt AC 93 ms 143792 KiB
02_handmade_01.txt AC 79 ms 144364 KiB
02_handmade_02.txt AC 80 ms 144268 KiB
02_handmade_03.txt AC 94 ms 143708 KiB
02_handmade_04.txt AC 94 ms 143696 KiB
02_handmade_05.txt AC 78 ms 143736 KiB
02_handmade_06.txt AC 78 ms 143752 KiB
02_handmade_07.txt AC 77 ms 143844 KiB
02_handmade_08.txt AC 77 ms 143624 KiB
02_handmade_09.txt AC 77 ms 143760 KiB
02_handmade_10.txt AC 78 ms 143844 KiB
02_handmade_11.txt AC 78 ms 143908 KiB
02_handmade_12.txt AC 79 ms 143880 KiB
02_handmade_13.txt AC 80 ms 144208 KiB
02_handmade_14.txt AC 81 ms 144280 KiB