C. 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 inlexicographical 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:si ≠ ti. 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 siand ti according to their order in alphabet.
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
3
rivest
shamir
adleman
bcdefghijklmnopqrsatuvwxyz
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Impossible
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
aghjlnopefikdmbcqrstuvwxyz
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
acbdefhijklmnogpqrstuvwxyz
两两比较字符串。如果前一个串是当前串的前缀则无需重排字母表;如果当前串是前一个串的前缀则发生错误,Impossible;否则就找到最左不同字符,并形成两结点,令前一个字符结点指向当前字符结点,最后再做一次拓扑排序。
1 /* 2 拓扑排序,跑一个队列 3 */ 4 #include <iostream> 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 #include <cmath> 9 #include <string> 10 #include <vector> 11 #include <set> 12 #include <map> 13 #include <queue> 14 #include <stack> 15 using namespace std; 16 const int INF = 0x7fffffff; 17 const double EXP = 1e-8; 18 const int MS = 105; 19 const int cnt = 26; 20 int n; 21 char str[MS][MS]; 22 bool link[cnt][cnt]; 23 int in[cnt]; //入度 24 char ans[cnt]; 25 bool have; 26 void input() 27 { 28 cin >> n; 29 for (int i = 0; i < n; i++) 30 cin >> str[i]; 31 32 memset(in, 0, sizeof(in)); 33 memset(link, false, sizeof(false)); 34 have = true; 35 for (int i = 0; i < n - 1&&have; i++) 36 { 37 int len1 = strlen(str[i]); 38 int len2 = strlen(str[i + 1]); 39 int ok = true; 40 for (int j = 0; j < len1&&j < len2&&ok; j++) 41 { 42 if (str[i][j] != str[i + 1][j]) 43 { 44 ok = false; 45 if (!link[str[i][j] - 'a'][str[i + 1][j] - 'a']) 46 { 47 link[str[i][j] - 'a'][str[i + 1][j] - 'a'] = true; 48 in[str[i + 1][j] - 'a']++; 49 } 50 } 51 } 52 if (ok&&len1 > len2) 53 { 54 have = false; 55 } 56 } 57 } 58 59 void solve() 60 { 61 if (!have) 62 { 63 cout << "Impossible" << endl; 64 return; 65 } 66 queue<int > que; 67 int num = 0; 68 for (int i = 0; i < cnt; i++) 69 if (in[i] == 0) 70 { 71 que.push(i); 72 ans[num++] = 'a' + i; 73 } 74 while (!que.empty()) 75 { 76 int s = que.front(); 77 que.pop(); 78 for (int i = 0; i < cnt; i++) 79 { 80 if (link[s][i]) 81 { 82 in[i]--; 83 if (in[i] == 0) 84 { 85 ans[num++] = 'a' + i; 86 que.push(i); 87 } 88 } 89 } 90 } 91 if (num < cnt) 92 cout << "Impossible" << endl; 93 else 94 { 95 ans[num] = '\0'; 96 cout << ans << endl; 97 } 98 } 99 100 int main() 101 { 102 input(); 103 solve(); 104 return 0; 105 }