-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.cpp
153 lines (144 loc) · 3.01 KB
/
D.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <bits/stdc++.h>
using namespace std;
const int MAX = 27;
const int MOD = 1000000007;
int a[MAX], sz[MAX], cnt[MAX];
bool head[MAX];
vector<string> root[MAX];
int find(int p) {
while (p != a[p]) {
a[p] = a[a[p]];
p = a[p];
}
return p;
}
bool connected(int p, int q) {
return find(p) == find(q);
}
void join(int p, int q) {
int i = find(p);
int j = find(q);
if (i != j) {
if (sz[i] < sz[j]) {
cnt[j] += cnt[i];
a[i] = j;
head[i] = false;
for (string s : root[i]) {
root[j].push_back(s);
}
}
else if (sz[i] > sz[j]) {
cnt[i] += cnt[j];
a[j] = i;
head[j] = false;
for (string s : root[j]) {
root[i].push_back(s);
}
}
else {
cnt[i] += cnt[j];
a[j] = i;
head[j] = false;
sz[i]++;
for (string s : root[j]) {
root[i].push_back(s);
}
}
}
}
void clear() {
for (int i = 0; i < MAX; i++) {
a[i] = i;
sz[i] = 0;
cnt[i] = 1;
head[i] = true;
}
}
void solve() {
clear();
int n;
cin >> n;
string s[n + 7];
for (int i = 0; i < n; i++) {
cin >> s[i];
bool vis[MAX] = {};
for (char c : s[i]) {
if (vis[(int)c - (int)'a']) {cout << "NO" << endl; return;}
else {vis[(int)c - (int)'a'] = true;}
}
}
for (int i = 0; i < n; i++) {
for (int j = 1; j < s[i].size(); j++) {
join((int)s[i][0] - (int)'a', (int)s[i][j] - (int)'a');
}
root[find((int)s[i][0] - (int)'a')].push_back(s[i]);
}
vector<string> res;
for (int i = 0; i < 26; i++) {
if (head[i] && !root[i].empty()) {
int to[MAX];
int from[MAX];
set<int> seen;
for (int i = 0; i < MAX; i++) {
to[i] = -1;
from[i] = -1;
}
for (string ss : root[i]) {
for (int i = 0; i < ss.size() - 1; i++) {
if (to[(int)ss[i] - (int)'a'] == -1 ||to[(int)ss[i] - (int)'a'] == (int)ss[i + 1] - (int)'a') {
to[(int)ss[i] - (int)'a'] = (int)ss[i + 1] - (int)'a';
from[(int)ss[i + 1] - (int)'a'] = (int)ss[i] - (int)'a';
}
else {cout << "NO" << endl; return;}
seen.insert((int)ss[i] - (int)'a');
seen.insert((int)ss[i + 1] - (int)'a');
}
}
string rn = "";
if (!seen.empty()) {
int head = -1;
for (int i : seen) {
if (from[i] == -1) {
if (head == -1) {head = i;}
else {cout << "NO" << endl; return;}
}
}
if (head == -1) {cout << "NO" << endl; return;}
int curr = head;
bool vis[MAX] = {};
while (curr != -1) {
if (!vis[curr]) {
rn += (char)((int)'a' + curr);
vis[curr] = true;
curr = to[curr];
}
else {cout << "NO" << endl; return;}
}
}
else {
for (string ss : root[i]) {
rn += ss;
}
}
res.push_back(rn);
}
}
for (string ss : res) {
bool vis[MAX] = {};
for (char c : ss) {
if (vis[(int)c - (int)'a']) {cout << "NO" << endl; return;}
else {vis[(int)c - (int)'a'] = true;}
}
}
sort(res.begin(), res.end());
for (string ss : res) {
cout << ss;
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
solve();
}