File tree Expand file tree Collapse file tree 6 files changed +292
-0
lines changed
Level 3 Class 1/Practice G1/Week 12 Expand file tree Collapse file tree 6 files changed +292
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ const int L = 26 ;
5
+
6
+ struct Node {
7
+ int cnt = 0 ;
8
+ Node *next[L]{};
9
+ };
10
+
11
+ void insert (Node *current, const string &s) {
12
+ for (char c : s) {
13
+ int i = c - ' a' ;
14
+ if (not current->next [i])
15
+ current->next [i] = new Node;
16
+ current = current->next [i];
17
+ ++current->cnt ;
18
+ }
19
+ }
20
+
21
+ int query (Node *current, const string &p) {
22
+ for (char c : p) {
23
+ int i = c - ' a' ;
24
+ if (not current->next [i])
25
+ return 0 ;
26
+ current = current->next [i];
27
+ }
28
+ return current->cnt ;
29
+ }
30
+
31
+ int main () {
32
+ ios::sync_with_stdio (false ); cout.tie (nullptr ); cin.tie (nullptr );
33
+ int n, Q;
34
+ Node root;
35
+ cin >> n >> Q;
36
+ for (int i = 0 ; i < n; ++i) {
37
+ string s; cin >> s;
38
+ insert (&root, s);
39
+ }
40
+ for (int i = 0 ; i < Q; ++i) {
41
+ string p; cin >> p;
42
+ cout << query (&root, p) << ' \n ' ;
43
+ }
44
+ return 0 ;
45
+ }
Original file line number Diff line number Diff line change
1
+ // This code uses boolean good-letter array.
2
+ #include < bits/stdc++.h>
3
+ using namespace std ;
4
+
5
+ const int L = 26 ;
6
+
7
+ struct Node {
8
+ Node *next[L]{};
9
+ };
10
+
11
+ int main () {
12
+ ios::sync_with_stdio (false ); cout.tie (nullptr ); cin.tie (nullptr );
13
+ string s; cin >> s;
14
+ bool is_good[26 ]{};
15
+ for (int i = 0 ; i < 26 ; ++i) {
16
+ char c; cin >> c;
17
+ is_good[i] = c - ' 0' ;
18
+ }
19
+ Node root;
20
+ int k, ans = 0 ; cin >> k;
21
+ for (int i = 0 ; i < s.size (); ++i) {
22
+ Node *current = &root;
23
+ int rem = k;
24
+ for (int j = i; j < s.size (); ++j) {
25
+ rem -= not is_good[s[j] - ' a' ];
26
+ if (rem < 0 )
27
+ break ;
28
+ if (not current->next [s[j] - ' a' ]) {
29
+ current->next [s[j] - ' a' ] = new Node;
30
+ ++ans;
31
+ }
32
+ current = current->next [s[j] - ' a' ];
33
+ }
34
+ }
35
+ cout << ans << ' \n ' ;
36
+ return 0 ;
37
+ }
Original file line number Diff line number Diff line change
1
+ // This code uses character good-letter array.
2
+ #include < bits/stdc++.h>
3
+ using namespace std ;
4
+
5
+ const int L = 26 ;
6
+
7
+ struct Node {
8
+ Node *next[L]{};
9
+ };
10
+
11
+ int main () {
12
+ ios::sync_with_stdio (false ); cout.tie (nullptr ); cin.tie (nullptr );
13
+ string s; cin >> s;
14
+ char good[L + 1 ];
15
+ cin >> good;
16
+ Node root;
17
+ int k, ans = 0 ; cin >> k;
18
+ for (int i = 0 ; i < s.size (); ++i) {
19
+ Node *current = &root;
20
+ int rem = k;
21
+ for (int j = i; j < s.size (); ++j) {
22
+ rem -= (good[s[j] - ' a' ] != ' 1' );
23
+ if (rem < 0 )
24
+ break ;
25
+ if (not current->next [s[j] - ' a' ]) {
26
+ current->next [s[j] - ' a' ] = new Node;
27
+ ++ans;
28
+ }
29
+ current = current->next [s[j] - ' a' ];
30
+ }
31
+ }
32
+ cout << ans << ' \n ' ;
33
+ return 0 ;
34
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ const int L = 26 ;
5
+
6
+ vector<string> us;
7
+
8
+ struct Node {
9
+ int ans_idx = -1 , ans_cnt = 0 ;
10
+ Node *next[L]{};
11
+ };
12
+
13
+ void insert (Node *current, const string &s) {
14
+ for (char c : s) {
15
+ int i = c - ' a' ;
16
+ if (not current->next [i])
17
+ current->next [i] = new Node;
18
+ current = current->next [i];
19
+ }
20
+ if (current->ans_idx == -1 ) {
21
+ current->ans_idx = (int ) us.size ();
22
+ us.push_back (s);
23
+ }
24
+ ++current->ans_cnt ;
25
+ }
26
+
27
+ void dfs (Node *current) {
28
+ for (int i = 0 ; i < L; ++i) {
29
+ if (not current->next [i])
30
+ continue ;
31
+ dfs (current->next [i]);
32
+ if (current->ans_cnt < current->next [i]->ans_cnt ) {
33
+ current->ans_cnt = current->next [i]->ans_cnt ;
34
+ current->ans_idx = current->next [i]->ans_idx ;
35
+ }
36
+ }
37
+ }
38
+
39
+ pair<int , int > query (Node *current, const string &pref) {
40
+ for (char c : pref) {
41
+ int i = c - ' a' ;
42
+ if (not current->next [i])
43
+ return {-1 , 0 };
44
+ current = current->next [i];
45
+ }
46
+ return {current->ans_idx , current->ans_cnt };
47
+ }
48
+
49
+ int main () {
50
+ ios::sync_with_stdio (false ); cout.tie (nullptr ); cin.tie (nullptr );
51
+ Node trie;
52
+ int n; cin >> n;
53
+ for (int i = 0 ; i < n; ++i) {
54
+ string s; cin >> s;
55
+ insert (&trie, s);
56
+ }
57
+ dfs (&trie);
58
+ int q; cin >> q;
59
+ while (q--) {
60
+ string pref;
61
+ cin >> pref;
62
+ auto [ans, cnt] = query (&trie, pref);
63
+ if (ans == -1 ) {
64
+ cout << " -1\n " ;
65
+ } else {
66
+ cout << us[ans] << ' ' << cnt << ' \n ' ;
67
+ }
68
+ }
69
+ return 0 ;
70
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ const int L = 26 ;
5
+
6
+ struct Node {
7
+ Node *next[L]{};
8
+ };
9
+
10
+ int main () {
11
+ ios::sync_with_stdio (false ); cout.tie (nullptr ); cin.tie (nullptr );
12
+ int tc; cin >> tc;
13
+ while (tc--) {
14
+ int n, nc; string s;
15
+ cin >> n >> nc >> s;
16
+ Node root;
17
+ int ans = 0 ;
18
+ for (int i = 0 ; i + n <= s.size (); ++i) {
19
+ Node *current = &root;
20
+ bool new_word = false ;
21
+ for (int j = i, e = i + n; j < e; ++j) {
22
+ int l = s[j] - ' a' ;
23
+ if (not current->next [l]) {
24
+ current->next [l] = new Node;
25
+ new_word = true ;
26
+ }
27
+ current = current->next [l];
28
+ }
29
+ ans += new_word;
30
+ }
31
+ cout << ans << ' \n ' ;
32
+ if (tc) cout << ' \n ' ;
33
+ }
34
+ return 0 ;
35
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ const int L = 26 , N = 1e5 + 5 ;
5
+
6
+ struct Node {
7
+ int cnt = 0 ;
8
+ Node *next[L]{};
9
+ };
10
+
11
+ multiset<int > ms[N];
12
+
13
+ void insert (Node *current, const string &s) {
14
+ int length = 0 ;
15
+ for (char c : s) {
16
+ int i = c - ' a' ;
17
+ if (not current->next [i])
18
+ current->next [i] = new Node;
19
+ current = current->next [i];
20
+ ++length;
21
+ multiset<int > &cur_ms = ms[length];
22
+ if (current->cnt > 0 )
23
+ cur_ms.erase (cur_ms.find (current->cnt ));
24
+ ++current->cnt ;
25
+ cur_ms.insert (current->cnt );
26
+ }
27
+ }
28
+
29
+ void erase (Node *current, const string &s) {
30
+ int length = 0 ;
31
+ for (char c : s) {
32
+ int i = c - ' a' ;
33
+ current = current->next [i];
34
+ ++length;
35
+ multiset<int > &cur_ms = ms[length];
36
+ cur_ms.erase (cur_ms.find (current->cnt ));
37
+ --current->cnt ;
38
+ if (current->cnt > 0 )
39
+ cur_ms.insert (current->cnt );
40
+ }
41
+ }
42
+
43
+ string words[N];
44
+
45
+ int main () {
46
+ ios::sync_with_stdio (false ); cout.tie (nullptr ); cin.tie (nullptr );
47
+ Node root;
48
+ int Q; cin >> Q;
49
+ for (int q = 1 ; q <= Q; ++q) {
50
+ int type; cin >> type;
51
+ if (type == 1 ) {
52
+ cin >> words[q];
53
+ reverse (words[q].begin (), words[q].end ());
54
+ insert (&root, words[q]);
55
+ continue ;
56
+ }
57
+ if (type == 3 ) {
58
+ int x; cin >> x;
59
+ if (not words[x].empty ()) {
60
+ erase (&root, words[x]);
61
+ words[x].clear ();
62
+ }
63
+ continue ;
64
+ }
65
+ int k, l;
66
+ cin >> k >> l;
67
+ auto iter = ms[l].lower_bound (k);
68
+ cout << (iter == ms[l].end () ? " NO\n " : " YES\n " );
69
+ }
70
+ return 0 ;
71
+ }
You can’t perform that action at this time.
0 commit comments