Description
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: 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 si and ti according to their order in alphabet.
Input
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.
Output
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).
Sample Input
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
标准的拓扑排序。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<algorithm> 6 using namespace std; 7 char s[105][105]; 8 queue<int> q; 9 int ans[105],in[105]; 10 int g[105][105]; 11 int cnt; 12 void toposort(){ 13 queue<int> q; 14 for(int i = 1; i<=26; i++) 15 if(!in[i]) q.push(i); 16 cnt = 0; 17 while(!q.empty()){ 18 int tmp = q.front(); 19 q.pop(); 20 ans[++cnt] = tmp; 21 for(int j = 1; j<=26; j++){ 22 if(g[tmp][j]){ 23 in[j]--; 24 if(!in[j]) q.push(j); 25 } 26 } 27 } 28 } 29 void input(){ 30 int n; 31 scanf("%d",&n); 32 for(int i = 1; i<=n; i++) scanf("%s",s[i]); 33 bool flag1 = true; 34 for(int i = 1; i<n; i++){ 35 int lena = strlen(s[i]); 36 int lenb = strlen(s[i+1]); 37 char a,b; 38 int tem1,tem2; 39 int flag = 0; 40 for(int j = 0; j<min(lena,lenb); j++){ 41 if(s[i][j]!=s[i+1][j]){ 42 a = s[i][j]; 43 b = s[i+1][j]; 44 tem1 = a - 'a' + 1; 45 tem2 = b - 'a' + 1; 46 if(!g[tem1][tem2]){ // 这没考虑到已经有路径的条件,一直WA 47 g[tem1][tem2] = 1; 48 in[tem2]++; 49 } 50 flag = 1; 51 break; 52 } 53 } 54 if(!flag&&lena>lenb){ 55 flag1 = false; 56 break; 57 } 58 } 59 if(!flag1) printf("Impossible\n"); 60 else{ 61 toposort(); 62 if(cnt<26) { 63 printf("Impossible\n"); 64 return; 65 } 66 for(int i = 1; i<=26; i++){ 67 printf("%c",ans[i]-1+'a'); 68 } 69 printf("\n"); 70 } 71 } 72 int main() 73 { 74 input(); 75 return 0; 76 }/* 77 26 78 a 79 b 80 c 81 d 82 e 83 f 84 g 85 h 86 i 87 j 88 k 89 l 90 m 91 n 92 o 93 p 94 q 95 r 96 s 97 t 98 u 99 v 100 w 101 x 102 y 103 z 104 */