File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Authored by : BaaaaaaaaaaarkingDog
2
+ // Co-authored by : -
3
+ // http://boj.kr/9c80cb818d834620a17dd09449948db1
4
+ #include < bits/stdc++.h>
5
+ using namespace std ;
6
+
7
+ const int ROOT = 1 ;
8
+ int unused = 2 ;
9
+ const int MX = 10000 * 500 + 5 ; // 최대 등장 가능한 글자의 수
10
+ bool chk[MX];
11
+ int nxt[MX][26 ];
12
+
13
+ int c2i (char c){
14
+ return c - ' a' ;
15
+ }
16
+
17
+ void insert (string& s){
18
+ int cur = ROOT;
19
+ for (auto c : s){
20
+ if (nxt[cur][c2i (c)] == -1 )
21
+ nxt[cur][c2i (c)] = unused++;
22
+ cur = nxt[cur][c2i (c)];
23
+ }
24
+ chk[cur] = true ;
25
+ }
26
+
27
+ bool find (string& s){
28
+ int cur = ROOT;
29
+ for (auto c : s){
30
+ if (nxt[cur][c2i (c)] == -1 )
31
+ return false ;
32
+ cur = nxt[cur][c2i (c)];
33
+ }
34
+ return chk[cur];
35
+ }
36
+
37
+ int n, m;
38
+ int main (void ){
39
+ ios::sync_with_stdio (0 );
40
+ cin.tie (0 );
41
+
42
+ for (int i = 0 ; i < MX; i++)
43
+ fill (nxt[i], nxt[i]+26 , -1 );
44
+
45
+ cin >> n >> m;
46
+ while (n--){
47
+ string s;
48
+ cin >> s;
49
+ insert (s);
50
+ }
51
+
52
+ int ans = 0 ;
53
+ while (m--){
54
+ string s;
55
+ cin >> s;
56
+ ans += find (s);
57
+ }
58
+ cout << ans;
59
+ }
You can’t perform that action at this time.
0 commit comments