forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.cpp
More file actions
95 lines (85 loc) · 2.7 KB
/
A.cpp
File metadata and controls
95 lines (85 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
int cnt[333], cur[333];
int n;
string a[222];
int id[33][10111];
int x[10111];
const int MN = 2000111;
char tr[MN];
int deg[MN];
vector<int> ke[MN];
int res[MN];
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
freopen("agnatic.in", "r", stdin);
freopen("agnatic.out", "w", stdout);
while (cin >> n) {
try {
memset(cnt, 0, sizeof cnt);
FOR(i,1,n) {
cin >> a[i];
memset(cur, 0, sizeof cur);
for(char c : a[i]) cur[c] += 1;
for(char c : a[i]) {
if (cnt[c] == 0) cnt[c] = cur[c];
else if (cnt[c] != cur[c]) throw 1;
}
}
int nid = 0;
FOR(c,'A','Z') if (cnt[c] > 0) {
FOR(t,1,cnt[c]) {
id[c - 'A'][t] = ++nid;
// cout << (char) c << ' ' << nid << endl;
tr[nid] = c;
}
}
FOR(i,1,nid) {
deg[i] = 0;
ke[i].clear();
}
FOR(i,1,n) {
memset(cur, 0, sizeof cur);
FOR(j,1,SZ(a[i])) {
char c = a[i][j-1];
++cur[c];
x[j] = id[c - 'A'][cur[c]];
}
// PR(x, SZ(a[i]));
FOR(j,1,SZ(a[i]) - 1) {
int u = x[j], v = x[j+1];
ke[u].push_back(v);
++deg[v];
}
}
queue<int> qu;
int nRes = 0;
FOR(i,1,nid) if (deg[i] == 0) {
qu.push(i);
}
while (!qu.empty()) {
int u = qu.front(); qu.pop();
res[++nRes] = u;
for(int v : ke[u]) {
--deg[v];
if (deg[v] == 0) qu.push(v);
}
}
if (nRes < nid) throw 2;
puts("YES");
FOR(i,1,nRes) putchar((char) tr[res[i]]);
puts("");
} catch (int e) {
puts("NO");
}
}
}