#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 1;
const int mod = 1e9 + 7;
int child[N][26], dp[N], depth[N], h[N], id[N], par[N];
map<int,int> pos[N];
set<int> chk[N];
int cnt = 0, n;
string s[N];
void insertword(string word,int num)
{
int node = 0, cur = 0;
chk[num].insert(0);
for(auto k : word)
{
if(child[node][k - 'a'] == 0)
{
child[node][k - 'a'] = ++cnt;
par[cnt] = node;
depth[cnt] = depth[node] + 1;
}
node = child[node][k - 'a'];
chk[num].insert(node);
pos[num][node] = cur + 1;
cur++;
}
h[num] = depth[node];
}
bool mmb(int a,int b)
{
return h[a] < h[b];
}
int32_t main()
{
// freopen("test.inp", "r" ,stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> s[i];
insertword(s[i], i);
id[i] = i;
}
vector<char> ans, curr;
sort(id + 1, id + n + 1, mmb);
int node = 0;
for(int i = 1; i <= n; i++)
{
while(chk[id[i]].find(node) == chk[id[i]].end()) node = par[node], ans.push_back('-');
for(int j = pos[id[i]][node]; j < s[id[i]].size(); j++)
{
node = child[node][s[id[i]][j] - 'a'];
ans.push_back(s[id[i]][j]);
}
ans.push_back('P');
}
cout << ans.size() <<'\n';
for(auto k : ans) cout << k<<'\n';
}
Compilation message
printer.cpp: In function 'int32_t main()':
printer.cpp:61:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int j = pos[id[i]][node]; j < s[id[i]].size(); j++)
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
129788 KB |
Output is correct |
2 |
Correct |
30 ms |
129880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
129884 KB |
Output is correct |
2 |
Incorrect |
27 ms |
129884 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
129880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
129872 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
129884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
132176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
140884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
155776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
125 ms |
193460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
105 ms |
182476 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |