Please sign in first.
Submission #65261443
Source Code Expand
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
// #define _GLIBCXX_DEBUG
// #define _GLIBCXX_DEBUG_PEDANTIC
#include <iomanip>
#include <cassert>
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <map>
#include <set>
#include <functional>
#include <array>
#include <numeric>
#include <queue>
#include <deque>
#include <bitset>
#include <cmath>
#include <climits>
#include <cstdint>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
const int MOD = 998244353;
const long double PI = 3.141592653589793;
using ll = long long;
const ll INF = 1e18;
// #define int ll
// --------> sashko123`s defines:
#define itn int //Vasya sorry :(
#define p_b push_back
#define fi first
#define se second
#define pii std::pair<int, int>
#define oo LLONG_MAX
#define big INT_MAX
#define elif else if
int input()
{
int x;
cin >> x;
return x;
}
// ----------> end of sashko123`s defines (thank you Vasya <3)
// template<typename K, typename V>
// using hash_table = cc_hash_table<K, V, hash<K>>;
// template<typename T>
// using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<int N = (int)1e6 + 1>
struct trie {
struct node {
int cnt = 0;
int terminate = 0;
array<int, 27> links;
node() {
links.fill(-1);
cnt = 0;
}
};
array<node, N> tree;
int sz = 0;
int root = 0;
trie() {
root = sz++;
}
void add(string s) {
int cur = root;
tree[cur].cnt++;
for (int i = 0; i < s.size(); i++) {
int nxt = tree[cur].links[s[i] - 'a'];
if (nxt == -1) {
nxt = sz++;
tree[cur].links[s[i] - 'a'] = nxt;
}
tree[nxt].cnt++;
cur = nxt;
}
tree[cur].terminate++;
}
int count(string s) const {
int cur = root;
for (int i = 0; i < s.size(); i++) {
int nxt = tree[cur].links[s[i] - 'a'];
if (nxt == -1) {
return 0;
}
cur = nxt;
if (tree[cur].terminate)
return 1;
}
return 0;
}
void remove(string s) {
int cur = root;
tree[cur].cnt--;
for (int i = 0; i < s.size(); i++) {
int nxt = tree[cur].links[s[i] - 'a'];
assert(nxt != -1);
tree[nxt].cnt--;
cur = nxt;
}
tree[cur].terminate--;
}
int get_cnt_of_str(const string& s) {
int cur = root;
int total = 0;
vector<int> path;
for (int i = 0; i < s.size(); i++) {
int nxt = tree[cur].links[s[i] - 'a'];
if (nxt == -1) {
return 0;
}
path.push_back(cur);
cur = nxt;
}
total = tree[cur].cnt;
tree[cur].cnt = 0;
tree[cur].terminate = 0;
tree[cur].links.fill(-1);
for (auto i: path) {
tree[i].cnt -= total;
}
return total;
}
void clear() {
for (int i = 0; i < sz; i++)
tree[i] = node();
}
};
trie s1, s2;
void solve() {
int n;
cin >> n;
int ans = 0;
int total = 0;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
if (t == 1) {
string x;
cin >> x;
ans += s1.get_cnt_of_str(x);
s2.add(x);
} else {
total++;
string s;
cin >> s;
if (s2.count(s)) {
ans += 1;
} else {
s1.add(s);
}
}
cout << total - ans << "\n";
}
}
int32_t main(int32_t argc, const char * argv[]) {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
// insert code here...
int tt= 1;
// std::cin >> tt;
for (int t = 1; t <= tt; t++) {
// cout << "Case #" << t << ": ";
solve();
}
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - Forbidden Prefix |
| User | VasyaMer |
| Language | C++ 20 (gcc 12.2) |
| Score | 500 |
| Code Size | 3886 Byte |
| Status | AC |
| Exec Time | 112 ms |
| Memory | 231320 KiB |
Compile Error
Main.cpp: In function ‘int32_t main(int32_t, const char**)’:
Main.cpp:193:22: warning: unused parameter ‘argc’ [-Wunused-parameter]
193 | int32_t main(int32_t argc, const char * argv[]) {
| ~~~~~~~~^~~~
Main.cpp:193:41: warning: unused parameter ‘argv’ [-Wunused-parameter]
193 | int32_t main(int32_t argc, const char * argv[]) {
| ~~~~~~~~~~~~~^~~~~~
Main.cpp: In instantiation of ‘int trie<N>::get_cnt_of_str(const std::string&) [with int N = 1000001; std::string = std::__cxx11::basic_string<char>]’:
Main.cpp:177:28: required from here
Main.cpp:137:35: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
137 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp: In instantiation of ‘void trie<N>::add(std::string) [with int N = 1000001; std::string = std::__cxx11::basic_string<char>]’:
Main.cpp:178:10: required from here
Main.cpp:95:35: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
95 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp: In instantiation of ‘int trie<N>::count(std::string) const [with int N = 1000001; std::string = std::__cxx11::basic_string<char>]’:
Main.cpp:183:16: required from here
Main.cpp:108:35: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
108 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 500 / 500 | ||||
| Status |
|
|
| 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 | 80 ms | 230020 KiB |
| 00_sample_02.txt | AC | 80 ms | 230016 KiB |
| 01_random_01.txt | AC | 85 ms | 230132 KiB |
| 01_random_02.txt | AC | 85 ms | 230136 KiB |
| 01_random_03.txt | AC | 86 ms | 230232 KiB |
| 01_random_04.txt | AC | 85 ms | 230096 KiB |
| 01_random_05.txt | AC | 86 ms | 230052 KiB |
| 01_random_06.txt | AC | 85 ms | 230152 KiB |
| 01_random_07.txt | AC | 86 ms | 230044 KiB |
| 01_random_08.txt | AC | 86 ms | 230088 KiB |
| 01_random_09.txt | AC | 88 ms | 230012 KiB |
| 01_random_10.txt | AC | 87 ms | 230024 KiB |
| 01_random_11.txt | AC | 86 ms | 230048 KiB |
| 01_random_12.txt | AC | 87 ms | 230100 KiB |
| 01_random_13.txt | AC | 87 ms | 229916 KiB |
| 01_random_14.txt | AC | 108 ms | 229952 KiB |
| 01_random_15.txt | AC | 110 ms | 229888 KiB |
| 01_random_16.txt | AC | 112 ms | 229988 KiB |
| 01_random_17.txt | AC | 100 ms | 230032 KiB |
| 01_random_18.txt | AC | 111 ms | 230040 KiB |
| 01_random_19.txt | AC | 83 ms | 230284 KiB |
| 01_random_20.txt | AC | 83 ms | 230308 KiB |
| 01_random_21.txt | AC | 87 ms | 230000 KiB |
| 01_random_22.txt | AC | 86 ms | 230124 KiB |
| 01_random_23.txt | AC | 88 ms | 230040 KiB |
| 01_random_24.txt | AC | 107 ms | 229868 KiB |
| 01_random_25.txt | AC | 98 ms | 229952 KiB |
| 01_random_26.txt | AC | 99 ms | 229992 KiB |
| 01_random_27.txt | AC | 91 ms | 230036 KiB |
| 01_random_28.txt | AC | 107 ms | 230020 KiB |
| 01_random_29.txt | AC | 98 ms | 230044 KiB |
| 01_random_30.txt | AC | 102 ms | 229888 KiB |
| 01_random_31.txt | AC | 94 ms | 229852 KiB |
| 01_random_32.txt | AC | 97 ms | 230180 KiB |
| 01_random_33.txt | AC | 87 ms | 230036 KiB |
| 01_random_34.txt | AC | 105 ms | 229964 KiB |
| 01_random_35.txt | AC | 97 ms | 229988 KiB |
| 01_random_36.txt | AC | 98 ms | 229996 KiB |
| 01_random_37.txt | AC | 110 ms | 230040 KiB |
| 01_random_38.txt | AC | 107 ms | 230040 KiB |
| 01_random_39.txt | AC | 105 ms | 230052 KiB |
| 01_random_40.txt | AC | 101 ms | 230004 KiB |
| 01_random_41.txt | AC | 99 ms | 230044 KiB |
| 02_handmade_01.txt | AC | 86 ms | 230636 KiB |
| 02_handmade_02.txt | AC | 87 ms | 230660 KiB |
| 02_handmade_03.txt | AC | 106 ms | 229992 KiB |
| 02_handmade_04.txt | AC | 108 ms | 230048 KiB |
| 02_handmade_05.txt | AC | 85 ms | 230040 KiB |
| 02_handmade_06.txt | AC | 86 ms | 229872 KiB |
| 02_handmade_07.txt | AC | 83 ms | 230064 KiB |
| 02_handmade_08.txt | AC | 84 ms | 229992 KiB |
| 02_handmade_09.txt | AC | 83 ms | 230064 KiB |
| 02_handmade_10.txt | AC | 85 ms | 230092 KiB |
| 02_handmade_11.txt | AC | 84 ms | 230132 KiB |
| 02_handmade_12.txt | AC | 88 ms | 230360 KiB |
| 02_handmade_13.txt | AC | 87 ms | 230344 KiB |
| 02_handmade_14.txt | AC | 92 ms | 231320 KiB |