-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSOLONG.cpp
More file actions
118 lines (97 loc) · 1.65 KB
/
Copy pathSOLONG.cpp
File metadata and controls
118 lines (97 loc) · 1.65 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <bits/stdc++.h>
using namespace std;
const int INF = 987654321;
struct wTree
{
bool end;
int no;
wTree *node[26];
int first;
wTree() : end(false), no(-1), first(-1)
{
memset(node, 0, sizeof(node));
}
~wTree()
{
for(int i=0; i < 26; i++)
if(node[i])
delete node[i];
}
void push(const char *str, int idx)
{
if(first == -1)
first = idx;
if(*str == 0)
{
no = idx;
return;
}
wTree *&next = node[*str-'A'];
if(next == 0)
next = new wTree;
return next->push(str+1, idx);
}
int find(const char *str)
{
if(*str == 0)
return no;
wTree *&next = node[*str-'A'];
if(next == 0)
return -2;
return next->find(str+1);
}
int type(const char *str, int idx)
{
if(first == idx)
return 1;
if(*str == 0 || node[*str-'A'] == 0)
return INF;
return 1 + node[*str-'A']->type(str+1, idx);
}
};
bool cmp(const pair<string,int>& p, const pair<string,int>& q)
{
if(p.second != q.second)
return p.second > q.second;
return p.first < q.first;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int N, M;
scanf("%d%d",&N,&M);
vector<pair<string,int>> a(N);
wTree trie;
for(int i=0; i < N; i++)
cin >> a[i].first >> a[i].second;
sort(a.begin(), a.end(), cmp);
for(int i=0; i < N; i++)
trie.push(a[i].first.c_str(), i);
int ans = 0;
while(M--)
{
char a[11];
scanf("%s",a);
int len = strlen(a);
int find = trie.find(a);
if(find < 0)
{
ans += len;
//printf("%d ", len);
}
else
{
int tab = trie.type(a, find) + (find == 0);
ans += min(len, tab);
//printf("%d ",tab);
}
ans++;
}
ans--;
printf("%d\n", ans);
}
return 0;
}