# Fox and Names

## 题目描述

Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: “Fox”). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn’t true. On some papers authors’ names weren’t sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare $s$ and $t$, first we find the leftmost position with differing characters: $s_i \neq t_i$. If there is no such position (i.e. $s$ is a prefix of $t$ or vice versa) the shortest string is less. Otherwise, we compare characters $s_i$ and $t_i$ according to their order in alphabet.

## 代码

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct author {
int id;
string name;
} a[101];
bool cmp(author a, author b) {
return a.name < b.name;
}
int n, in[26];
bool graph[26][26];
queue<int> que;
string ans;
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].name;
a[i].id = i;
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i < n; ++i) {
for (int j = i + 1; j <= n; ++j) {
int p = 0, len1 = a[i].name.length(), len2 = a[j].name.length();
while (p < len1 && p < len2 && a[i].name[p] == a[j].name[p]) ++p;
if (p == len1 && p < len2 && a[i].id > a[j].id) {
cout << "Impossible" << endl;
return 0;
}
if (p < len1) {
if (a[i].id < a[j].id) graph[a[i].name[p] - 'a'][a[j].name[p] - 'a'] = true;
else graph[a[j].name[p] - 'a'][a[i].name[p] - 'a'] = true;
}
}
}
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < 26; ++j) {
if (graph[i][j]) ++in[j];
}
}
for (int i = 0; i < 26; ++i) {
if (!in[i]) que.push(i);
}
while (!que.empty()) {
int t = que.front();
que.pop();
for (int i = 0; i < 26; ++i) {
if (graph[t][i]) {
--in[i];
if (!in[i]) que.push(i);
}
}
ans += t + 'a';
}
if (ans.length() < 26) {
cout << "Impossible" << endl;
} else {
cout << ans << endl;
}
return 0;
}


418 I'm a teapot