/** I can do this all day **/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
const int N = 5e5 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;
const int sigma = 26;
ll pw(ll a , ll b, ll M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }
char C[N];
int n, ts, node, H[N], End[N], nxt[sigma][N], par[N], mark[N];
void dfs(int v)
{
if(End[v])
{
printf("P\n");
}
for(int i = 0; i < sigma; i ++)
{
if(nxt[i][v] == 0 || mark[nxt[i][v]]) continue;
char c = 'a' + i;
printf("%c\n", c);
dfs(nxt[i][v]);
printf("-\n");
}
if(mark[v])
{
for(int i = 0; i < sigma; i ++)
{
if(nxt[i][v] && mark[nxt[i][v]])
{
char c = 'a' + i;
printf("%c\n", c);
dfs(nxt[i][v]);
}
}
}
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++)
{
scanf("%s", C);
string s = C;
int v = 0;
for(int j = 0; j < SZ(s); j ++)
{
int ch = s[j] - 'a';
if(!nxt[ch][v]) nxt[ch][v] = ++ ts;
H[nxt[ch][v]] = H[v] + 1;
par[nxt[ch][v]] = v;
v = nxt[ch][v];
}
End[v] ++;
if(H[v] > H[node]) node = v;
///printf("i = %d H = %d v = %d node = %d\n", i, H[v], v, node);
}
while(node)
{
///printf("marking node = %d\n", node);
mark[node] = 1;
node = par[node];
}
mark[0] = 1;
dfs(0);
return 0;
}
/** test corner cases(n = 1?) watch for overflow or minus indices **/
Compilation message
printer.cpp: In function 'int main()':
printer.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
62 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
printer.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%s", C);
| ~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Expected integer, but "t" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Expected integer, but "e" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Expected integer, but "h" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Expected integer, but "b" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
588 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1228 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
3480 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
7968 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
60 ms |
19396 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
15244 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |