#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool minn(T &A,T B){return A > B ? (A = B,1) : 0;}
template <class T> inline bool maxx(T &A,T B){return A < B ? (A = B,1) : 0;}
#define pb push_back
#define endl '\n'
#define SZ(x) (int)((x).size())
#define dem(x) __builtin_popcount(x)
#define fi first
#define se second
//#define int long long
typedef pair<int,int> ii;
typedef long long ll;
const ll mod = 1e9 + 7; //998244353
vector<int> a[800005];
char c[800005], ans[10000005];
string s;
int d[800005], n, best[800005], cnt, t;
bool dd[800005];
void add(int id,string s,int it)
{
if(it == SZ(s)){
dd[id] = 1;
return;
}
bool ck = 1;
for(int i : a[id])if(c[i] == s[it]){
ck = 0;
add(i, s, it + 1);
if(maxx(d[id], d[i] + 1))best[id] = i;
break;
}
if(ck){
cnt++;
a[id].pb(cnt);
c[cnt] = s[it];
add(cnt, s, it + 1);
if(maxx(d[id], d[a[id].back()] + 1))best[id] = a[id].back();
}
}
void DFS(int id)
{
if(id != 0)ans[++t] = c[id];
if(dd[id]){
cnt++;
ans[++t] = 'P';
if(cnt == n){
cout << t << endl;
for(int i = 1;i < t;++i)cout << ans[i] << endl;
cout << ans[t];
exit(0);
}
if(SZ(a[id]) == 0){
ans[++t] = '-';
return;
}
}
for(int i : a[id])if(i != best[id])DFS(i);
DFS(best[id]);
ans[++t] = '-';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// freopen("c.inp","r",stdin);
cin >> n;
for(int i = 0;i < n;++i){
cin >> s;
add(0, s, 0);
}
cnt = 0;
DFS(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
19020 KB |
Output is correct |
2 |
Correct |
12 ms |
19020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
19020 KB |
Output is correct |
2 |
Correct |
12 ms |
19020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
19068 KB |
Output is correct |
2 |
Correct |
12 ms |
19020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
19020 KB |
Output is correct |
2 |
Correct |
11 ms |
19128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
19160 KB |
Output is correct |
2 |
Correct |
13 ms |
19276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
19440 KB |
Output is correct |
2 |
Correct |
16 ms |
19500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
20184 KB |
Output is correct |
2 |
Correct |
27 ms |
21428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
21836 KB |
Output is correct |
2 |
Correct |
27 ms |
19660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
26184 KB |
Output is correct |
2 |
Correct |
174 ms |
35780 KB |
Output is correct |
3 |
Correct |
92 ms |
27460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
24372 KB |
Output is correct |
2 |
Correct |
210 ms |
39004 KB |
Output is correct |
3 |
Correct |
105 ms |
28548 KB |
Output is correct |
4 |
Correct |
134 ms |
37920 KB |
Output is correct |