#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forsn(i, s, n) for (int i = int(s); i < int(n); i++)
#define all(x) x.begin(), x.end()
#define LSB(x) x & -x
typedef long long ll;
using namespace std;
/**
Trie
traverse in sorted order
**/
int trie[500020][27], d[500020], nxt = 1, rta = 0;
bool isWord[500020], dont[500020];
string longest = "";
void insert(string &s)
{
int i = 0, v = 0;
while (i < s.size())
{
if (trie[v][(s[i]-'a')] == -1)
{
trie[v][(s[i]-'a')] = nxt++;
}
v = trie[v][(s[i++]-'a')];
}
isWord[v] = true;
}
void depths(int node, int depth)
{
d[node] = depth;
forn(i, 26)
{
if (trie[node][i] != -1)
{
depths(trie[node][i], depth + 1);
}
}
}
void dfs(int node)
{
int d_it = -1;
forn(i, 26)
{
if (trie[node][i] != -1)
{
if (!dont[trie[node][i]])
{
rta++;
//cout << (char)(i + 'a') << "\n";
if(isWord[trie[node][i]]){
//cout << "P\n";
rta++;
}
dfs(trie[node][i]);
rta++;
//cout << "-\n";
}
else
d_it = i;
}
}
if (d_it != -1){
rta++;
//cout << (char)(d_it + 'a') << "\n";
if(isWord[trie[node][d_it]]){
//cout << "P\n";
rta++;
}
dfs(trie[node][d_it]);
}
}
int main()
{
iostream::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
memset(trie, -1, sizeof(trie));
memset(isWord, false, sizeof(isWord));
memset(dont, false, sizeof(dont));
forn(i, n)
{
string s;
cin >> s;
insert(s);
if (s.size() > longest.size())
{
longest = s;
}
}
int v = 0;
forn(i, longest.size())
{
v = trie[v][longest[i] - 'a'];
dont[v] = true;
}
// declare depths
depths(0, 0);
// dfs
dfs(0);
// display
cout << rta;
return 0;
}
/*
*/
Compilation message
printer.cpp: In function 'void insert(std::string&)':
printer.cpp:21:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | while (i < s.size())
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
54092 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
54084 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
54020 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
54112 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
54120 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
54088 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
54172 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
54428 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
54968 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
54816 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |