#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
#define fi first
#define se second
#define _for(ccc, sss, eee) for (int ccc=(sss);ccc<=(eee);ccc++)
#define __for(ccc, sss, eee) for (int ccc=(sss);ccc>=(eee);ccc--)
#define N 25000
#define M 26
#define INF 0x7f7f7f7f
struct node{
int son[M];
bool is_lest, is_end;
// 是最长的 是一个单词
}pool[N*20];//一共25000个词语, 每个词语最长20
int sz=0;//大小
inline void ins(const string &x){
int cur=0;
for (auto &c:x){
int &nex=pool[cur].son[c-'a'];
if (!nex)nex=++sz;
cur=nex;
}
pool[cur].is_end=true;
}
void DFS(int cur, string &ans){
const node &x=pool[cur];
if (x.is_end)ans+='P';
int ind=-1, nex;
_for (i, 0, M-1){
nex=x.son[i];//下一个节点
if (!nex)continue;//没有
if (pool[nex].is_lest)ind=i;
else ans+=i+'a', DFS(nex, ans);
}
if (ind!=-1)ans+=ind+'a', DFS(x.son[ind], ans);
ans+='-';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);//如果写freopen()删掉cout.tie()
int n;
string s, lest;
cin>>n;
_for (i, 0, n-1){
cin>>s, ins(s);
if (s.size()>lest.size())lest=s;
}
int last=0;
_for (i, 0, lest.size()-1){//标记最后走
last=pool[last].son[lest[i]-'a'];
pool[last].is_lest=true;
}
s.clear();
DFS(0, s);
while (*(s.end()-1)=='-')s.pop_back();//最后不用退出
cout<<s.size()<<'\n';
for (auto &c:s)cout<<c<<'\n';
return 0;
}
Compilation message
printer.cpp: In function 'int main()':
printer.cpp:10:51: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | #define _for(ccc, sss, eee) for (int ccc=(sss);ccc<=(eee);ccc++)
| ^
printer.cpp:55:2: note: in expansion of macro '_for'
55 | _for (i, 0, lest.size()-1){//标记最后走
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1112 KB |
Output is correct |
2 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3164 KB |
Output is correct |
2 |
Correct |
10 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
7664 KB |
Output is correct |
2 |
Correct |
5 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
20108 KB |
Output is correct |
2 |
Correct |
62 ms |
41556 KB |
Output is correct |
3 |
Correct |
31 ms |
21488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
14328 KB |
Output is correct |
2 |
Correct |
72 ms |
49424 KB |
Output is correct |
3 |
Correct |
39 ms |
24296 KB |
Output is correct |
4 |
Correct |
59 ms |
46604 KB |
Output is correct |