CF Fox And Names (拓扑排序)

Fox And Names
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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 test(s)
input
3
rivest
shamir
adleman
output
bcdefghijklmnopqrsatuvwxyz
input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
output
Impossible
input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
output
aghjlnopefikdmbcqrstuvwxyz
input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
output
acbdefhijklmnogpqrstuvwxyz

 

 

拓扑排序第一题,注意特判一下下面的串是上面的串的前缀这种情况,直接输出Impossible了。

 1 #include <bits/stdc++.h>
 2 using    namespace    std;
 3 
 4 bool    G[30][30];
 5 int    IN[30];
 6 int    ANS[30];
 7 
 8 void    toposort(void);
 9 int    main(void)
10 {
11     int    n;
12     char    name[105][105];
13     bool    flag;
14 
15     cin >> n;
16     for(int i = 0;i < n;i ++)
17         cin >> name[i];
18     for(int i = 0;i < n - 1;i ++)
19     {
20         for(int j = 0;name[i][j] && name[i + 1][j];j ++)
21         {
22             flag = false;
23             if(name[i][j] != name[i + 1][j])
24             {
25                 if(!G[name[i][j] - 'a'][name[i + 1][j] - 'a'])
26                 {
27                     G[name[i][j] - 'a'][name[i + 1][j] - 'a'] = true;
28                     IN[name[i + 1][j] - 'a'] ++;
29                 }
30                 flag = true;
31                 break;
32             }
33         }
34         if(!flag && strlen(name[i]) > strlen(name[i + 1]))
35         {
36             puts("Impossible");
37             return    0;
38         }
39     }
40     toposort();
41 
42     return    0;
43 }
44 
45 void    toposort(void)
46 {
47     queue<int>    que;
48     int    sum = 1;
49     int    k = 0;
50     int    front;
51 
52     for(int i = 0;i < 26;i ++)
53         if(!IN[i])
54         {
55             ANS[k ++] = i;
56             sum ++;
57             que.push(i);
58         }
59 
60     while(!que.empty())
61     {
62         front = que.front();
63         que.pop();
64         for(int i = 0;i < 26;i ++)
65             if(G[front][i])
66             {
67                 IN[i] --;
68                 if(!IN[i])
69                 {
70                     ANS[k ++] = i;
71                     que.push(i);
72                     sum ++;
73                 }
74             }
75     }
76     if(sum < 26)
77         puts("Impossible");
78     else
79     {
80         for(int i = 0;i < 26;i ++)
81             printf("%c",ANS[i] + 'a');
82         puts("");
83     }
84 
85     return    ;
86 }

 

posted @ 2015-04-24 11:06  Decouple  阅读(232)  评论(0编辑  收藏  举报