This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int N;
string V[101];
struct TrieNode{
int nrcuv,nrfii;
TrieNode *fii[26];
TrieNode(){
nrcuv=nrfii=0;
for(int i=0;i<26;i++)
fii[i]=0;
}
};
TrieNode *root = new TrieNode;
void inserare(TrieNode *nod,char *s){
if(*s==0){
nod->nrcuv++;
return;
}
if(nod->fii[*s-'a']==0){
nod->nrfii++;
nod->fii[*s-'a'] = new TrieNode;
}
inserare(nod->fii[*s-'a'],s+1);
}
char cuvmax[21];
int rez;
int cont;
string S;
void f(TrieNode *nod,string s,char *cuv){
int p;
for(int i=0;i<26;i++){
if(nod->fii[i]==0) continue;
if((char)(i+'a')==*cuv){
p=i;
continue;
}
S+=(char)(i+'a');
if(nod->fii[i]->nrcuv!=0)
S+='P',nod->fii[i]->nrcuv=0,cont++;
f(nod->fii[i],s+(char)(i+'a'),cuv+1);
if(cont<N) S+='-';
}
if(nod->fii[p]==0) return;
S+=(char)(p+'a');
if(nod->fii[p]->nrcuv!=0)
S+='P',cont++;
f(nod->fii[p],s+(char)(p+'a'),cuv+1);
if(cont<N) S+='-';
}
int main()
{
cin >> N;
for(int i=1;i<=N;i++){
char s[21];
cin >> s;
inserare(root,s);
if(strlen(s)>strlen(cuvmax))
strcpy(cuvmax,s);
}
f(root,"",cuvmax);
cout << S.size() << '\n';
for(auto it:S) cout << it << '\n';
return 0;
}
Compilation message (stderr)
printer.cpp: In function 'void f(TrieNode*, std::string, char*)':
printer.cpp:55:16: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
55 | S+=(char)(p+'a');
| ~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |