#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mod 1000000007
#define mxe(v) *max_element(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(),v.rend()
#define F first
#define S second
#define pi pair<ll,ll>
#define assem ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
using namespace std;
const int N=400200;
const double PI=3.1415926535897932384626433;
const double eps = 1e-9;
string getString()
{
char c[(int)50];
//scanf(" %c",&a[i]);
scanf("%s", c);
return c;
//string x = getString();
}
///printf("%d\n",);
///scanf("%d",&);
///cout.precision(10);
///puts("")string
vector<char> ans;
int n;
vector<string> v;
struct trie
{
int cnt,c;
vector<char> a;
unordered_map<int,trie*>child;
trie()
{
cnt=c=0;
//memset(child,0,sizeof child);
}
void add(int i,int j,int sz)
{
if(j==sz)
{
c++;
cnt=j;
return;
}
if(child[v[i][j]-'a']==0)
{
child[v[i][j]-'a']=new trie();
child[v[i][j]-'a']->add(i,j+1,sz);
a.push_back(v[i][j]);
}
else
{
child[v[i][j]-'a']->add(i,j+1,sz);
}
if(child[v[i][j]-'a']->cnt>cnt)cnt=child[v[i][j]-'a']->cnt;
}
void get()
{
while(c)
{
ans.push_back('P');
c--;
}
bool f=1;
char id='a';
int nn=a.size();
for(int i=0;i<a.size(); i++)
{
if(f&&child[a[i]-'a']!=0&&child[a[i]-'a']->cnt==cnt)
{
f=0;
id=a[i];
}
else if(child[a[i]-'a']!=0)
{
ans.push_back(a[i]);
child[a[i]-'a']->get();
ans.push_back('-');
}
}
if(!f)
{
ans.push_back(id);
child[id-'a']->get();
ans.push_back('-');
}
return;
}
};
int main()
{
scanf("%d",&n);
trie* root=new trie();
for(int i=0; i<n; i++)
{
string q;
q=getString();
v.push_back(q);
root->add(i,0,q.size());
}
root->get();
while(ans.back()=='-')ans.pop_back();
//printf("%d\n",(int)ans.size());
int nn=ans.size();
cout<<nn<<endl;
for(int i=0; i<nn;i++)
{
//cout<<ans[i]<<endl;
printf("%c\n",ans[i]);
}
}
Compilation message
printer.cpp: In member function 'void trie::get()':
printer.cpp:73:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0;i<a.size(); i++)
| ~^~~~~~~~~
printer.cpp:72:13: warning: unused variable 'nn' [-Wunused-variable]
72 | int nn=a.size();
| ^~
printer.cpp: In function 'std::string getString()':
printer.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%s", c);
| ~~~~~^~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2004 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2516 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
6752 KB |
Output is correct |
2 |
Correct |
34 ms |
14408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
16896 KB |
Output is correct |
2 |
Correct |
15 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
42744 KB |
Output is correct |
2 |
Correct |
181 ms |
98952 KB |
Output is correct |
3 |
Correct |
112 ms |
50396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
32280 KB |
Output is correct |
2 |
Correct |
232 ms |
117696 KB |
Output is correct |
3 |
Correct |
124 ms |
57144 KB |
Output is correct |
4 |
Correct |
231 ms |
110972 KB |
Output is correct |