-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trie.cpp
97 lines (67 loc) · 896 Bytes
/
Trie.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int M = 1000000007;
const int N=100003;
LL trie[26][N],word[N],prefix[N];
LL next;
void add(string &s)
{
int i=0,v=0;
while(i<s.size())
{
if(trie[s[i]-'a'][v]==-1)
v=trie[s[i++]-'a'][v]=next++;
else
v=trie[s[i++]-'a'][v];
prefix[v]++;
}
word[v]++;
}
void get(string &s)
{
int i=0,v=0;
int ma = s.size();
while(i<s.size())
{
if(trie[s[i]-'a'][v]!=-1)
v=trie[s[i]-'a'][v];
else
{
ma = max(ma,i);
break;
}
ma = max(ma,i+prefix[v]);
i++;
}
return ma;
}
int main()
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<26;j++)
trie[j][i]=-1;
}
next=1;
int n,m;
cin >> n >> m;
string s;
vector <string> v(n);
for(i=0;i<n;i++)
{
cin >> v[i];
}
int ans = 0;
for(i=0;i<m;i++)
{
cin >> s;
aadd(s);
}
for(i=0;i<n;i++)
{
ans += get(v[i]);
}
cout<<ans<<endl;
}