#include <bits/stdc++.h>
using namespace std;
const int maxn = 26;
vector<int> adj[maxn];
int id[maxn];
bool edge[maxn][maxn];
int ch(char c) {
return c - 'a';
}
char ch3(int idx) {
return 'a' + idx;
}
int n;
bool pos = false;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
memset(id, 0, sizeof(id));
memset(edge, false, sizeof(edge));
cin >> n;
for (int i = 0; i < n; ++i) {
string a, b;
cin >> a >> b;
int mlen = min(a.size(), b.size());
int k;
for (k = 0; k < mlen; ++k) {
if (a[k] != b[k]) {
break;
}
}
if (k == mlen) {
if (a.size() > b.size()) {
pos = true;
}
continue;
}
int u = ch(a[k]);
int v = ch(b[k]);
if (!edge[u][v]) {
edge[u][v] = true;
adj[u].push_back(v);
id[v]++;
}
}
if (pos) {
cout << -1 << endl;
return 0;
}
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < maxn; ++i) {
if (id[i] == 0) {
pq.push(i);
}
}
string res;
while (!pq.empty()) {
int u = pq.top();
pq.pop();
res += ch3(u);
for (int v : adj[u]) {
id[v]--;
if (id[v] == 0) {
pq.push(v);
}
}
}
if (res.size() != maxn) {
cout << -1 << endl;
}
else {
cout << res << endl;
}
return 0;
}剪贴板
剪贴板 y92aqt2z
剪贴板信息
剪贴板信息
- 收录
- 更新