-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcellphoneTyping_UVA12526.cpp
99 lines (78 loc) · 1.93 KB
/
cellphoneTyping_UVA12526.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
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cellphoneTyping_UVA12526.in");
ofstream fout("cellphoneTyping_UVA12526.out");
const int nc = 1000000, cc = 26;
class trie
{
private:
int num = 1;
vector<int> wordCount;
vector<vector<int>> a, g;
int ctoi(char ch)
{
return ch - 'a';
}
public:
trie()
{
wordCount.assign(nc, 0);
a.assign(nc, vector<int>(cc, -1));
g.assign(nc, vector<int>());
}
void insert(const string & s)
{
int size = s.size(), last = 0;
for (int i = 0; i <= size - 1; ++i)
{
if (a[last][ctoi(s[i])] == -1)
{
a[last][ctoi(s[i])] = num;
g[last].push_back(ctoi(s[i]));
++num;
}
last = a[last][ctoi(s[i])];
if (i == size - 1)
{
++wordCount[last];
}
}
}
int count(const string & s)
{
int size = s.size(), last = 0, res = 0;
for (int i = 0; i <= size - 1; ++i)
{
if (i == 0 || g[last].size() > 1 || wordCount[last] > 0)
{
++res;
}
last = a[last][ctoi(s[i])];
}
return res;
}
};
int main()
{
fout << fixed << setprecision(2);
while (true)
{
int n = 0; fin >> n;
if (n == 0) break;
vector<string> dictionary(n);
trie t;
for (int i = 0; i <= n - 1; ++i)
{
fin >> dictionary[i];
t.insert(dictionary[i]);
}
int ans = 0;
for (int i = 0; i <= n - 1; ++i)
{
ans += t.count(dictionary[i]);
}
double ans_d = (double)ans / (double)n;
fout << (int)(ans_d * 100 + 0.5) / 100.0 << '\n';
}
return 0;
}