#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()){//标记最后走
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()){//标记最后走
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3164 KB |
Output is correct |
2 |
Correct |
9 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7660 KB |
Output is correct |
2 |
Correct |
5 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
20460 KB |
Output is correct |
2 |
Correct |
62 ms |
41528 KB |
Output is correct |
3 |
Correct |
30 ms |
21492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
14328 KB |
Output is correct |
2 |
Correct |
74 ms |
49420 KB |
Output is correct |
3 |
Correct |
38 ms |
24308 KB |
Output is correct |
4 |
Correct |
60 ms |
46956 KB |
Output is correct |