UVA-200 Rare Order 拓扑排序
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Rare Order |
A rare book collector recently discovered a book written in an unfamiliar language that used the same characters as the English language. The book contained a short index, but the ordering of the items in the index was different from what one would expect if the characters were ordered the same way as in the English alphabet. The collector tried to use the index to determine the ordering of characters (i.e., the collating sequence) of the strange alphabet, then gave up with frustration at the tedium of the task.
You are to write a program to complete the collector's work. In particular, your program will take a set of strings that has been sorted according to a particular collating sequence and determine what that sequence is.
Input
The input consists of an ordered list of strings of uppercase letters, one string per line. Each string contains at most 20 characters. The end of the list is signalled by a line with the single character `#'. Not all letters are necessarily used, but the list will imply a complete ordering among those letters that are used.
Output
Your output should be a single line containing uppercase letters in the order that specifies the collating sequence used to produce the input data file.
Sample Input
XWY ZX ZXY ZXW YWWX #
Sample Output
XZYW
给定一个新的规则,输出新规则下的“字典序”。题中保证一定会合法。
代码如下:
1 #include <cstring>
2 #include <cstdlib>
3 #include <cstdio>
4 #include <queue>
5 using namespace std;
6
7 char ss[1000000][25];
8
9 int map[30][30], here[30], hash[30];
10
11 inline void sscmp( char *a, char *b )
12 {
13 int len1 = strlen( a ), len2 = strlen( b );
14 int bound = len1 < len2 ? len1 : len2;
15 for( int i = 0; i < bound; ++i )
16 {
17 if( a[i] != b[i] )
18 {
19 // printf( "%c %c\n", a[i], b[i] );
20 map[ a[i] - 'A' ][ b[i] - 'A' ] = 1;
21 here[ a[i] - 'A' ] = here[ b[i] - 'A' ] = 1;
22 break;
23 }
24 }
25 }
26
27 inline void search( int k )
28 {
29 for( int i = 0; i < k - 1; ++i )
30 {
31 sscmp( ss[i], ss[i + 1] );
32 }
33 }
34
35 int find()
36 {
37 for( int i = 0; i < 26; ++i )
38 {
39 if( here[i] && !hash[i] )
40 {
41 int j;
42 for( j = 0; j < 26; ++j )
43 {
44 if( map[j][i] != 0 )
45 break;
46 }
47 if( j == 26 )
48 {
49 return i;
50 }
51 }
52 }
53 return -1;
54 }
55
56 void topsort( int rec[], int &cnt )
57 {
58 queue<int>q;
59 int f = find();
60 if( f != -1 )
61 {
62 hash[f] = 1;
63 q.push( f );
64 }
65 else
66 return;
67 while( !q.empty() )
68 {
69 int pos = q.front();
70 rec[cnt++] = pos;
71 q.pop();
72 int i;
73 for( i = 0; i < 26; ++i )
74 {
75 map[pos][i] = 0;
76 }
77 f = find();
78 if( f != -1 )
79 {
80 hash[f] = 1;
81 q.push( f );
82 }
83 else
84 return;
85 }
86 }
87
88 int main()
89 {
90 int k, rec[30], cnt = 0;
91 for( k = 0; ; ++k )
92 {
93 scanf( "%s", ss[k] );
94 if( ss[k][0] == '#' )
95 break;
96 }
97 search( k );
98 topsort( rec, cnt );
99 for( int i = 0; i < cnt; ++i )
100 printf( i == cnt - 1 ? "%c\n" : "%c", rec[i] + 'A' );
101 }