-
Notifications
You must be signed in to change notification settings - Fork 0
/
huawei5.cpp
103 lines (91 loc) · 2.04 KB
/
huawei5.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
98
99
100
101
102
103
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using op_t = pair<char, string>;
class WhitelistMgr
{
private:
unordered_set<string> nml_lst_;
unordered_set<string> reg_lst_;
bool match_prefix_(const string& prefix, const string& target)
{
for (size_t i = 0; i < prefix.size(); ++i)
{
if (prefix[i] != target[i]) return false;
}
return true;
}
public:
bool is_in_white_list(const string& number)
{
if (nml_lst_.find(number) != nml_lst_.end()) return true;
for (const string& e : reg_lst_)
{
if (match_prefix_(e, number)) return true;
}
return false;
}
void add_white_list(const string& number)
{
if (number.back() == '*')
{
string x = number;
x.pop_back();
reg_lst_.emplace(x);
}
else
{
nml_lst_.emplace(number);
}
}
};
int main()
{
int n;
char c;
string s;
cin >> n;
vector<op_t> ops;
ops.reserve(n);
while (n--)
{
cin >> c >> s;
ops.emplace_back(c, s);
}
// io done
WhitelistMgr wm;
vector<string> call_order;
unordered_map<string, pair<int, int>> records;
for (auto &op : ops)
{
if (op.first == 'W')
{
wm.add_white_list(op.second);
}
else if (op.first == 'C')
{
if (records.find(op.second) == records.end())
{
records[op.second] = {0, 0};
call_order.emplace_back(op.second);
}
if (wm.is_in_white_list(op.second))
{
++records[op.second].first;
}
else
{
++records[op.second].second;
}
}
}
// output
for (auto &e : call_order)
{
cout << e << " " << records[e].first << " " << records[e].second << endl;
}
return 0;
}