이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n, cnt;
string s;
struct trie
{
struct node
{
char c;
int type;
node* nxt[27];
node* p;
node(char ch, node* pa)
{
c=ch;
p=pa;
type=0;
for (int i=0; i<27; i++) nxt[i]=NULL;
}
};
typedef node* pnode;
pnode rt=new node('a', NULL);
vector<pair<int, pnode>> v;
void update(string &s)
{
pnode* c=&rt;
for (auto x:s)
{
if ((*c)->nxt[x-'a']) c=&((*c)->nxt[x-'a']);
else (*c)->nxt[x-'a']=new node(x, *c), c=&((*c)->nxt[x-'a']);
}
}
void dfssz(pnode k, int lvl)
{
v.push_back({lvl, k});
for (int i=0; i<27; i++) if (k->nxt[i]) dfssz(k->nxt[i], lvl+1);
}
void calcheavy()
{
pair<int, pnode> t;
for (auto x:v) t=max(t, x);
//cout<<t.first<<' '<<t.second->c<<'\n';
while (t.second!=NULL) t.second->type=1, t.second=t.second->p;
}
void print(pnode k)
{
bool pt=0;
for (int i=0; i<27; i++) if (k->nxt[i]&&k->nxt[i]->type!=1) cout<<(char)('a'+i)<<'\n', pt=1, print(k->nxt[i]);
for (int i=0; i<27; i++) if (k->nxt[i]&&k->nxt[i]->type==1) cout<<(char)('a'+i)<<'\n', pt=1, print(k->nxt[i]);
if (!pt) cout<<"P\n";
if (k->type) return;
cout<<"-\n";
}
} d;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
while (n--) cin>>s, d.update(s);
d.dfssz(d.rt, 0);
d.calcheavy();
d.print(d.rt);
}
# | 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... |