题目传送门
1 /*
2 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序
3 拓扑排序
4 详细解释:http://www.2cto.com/kf/201502/374966.html
5 */
6 #include <cstdio>
7 #include <iostream>
8 #include <cstring>
9 #include <string>
10 #include <map>
11 #include <algorithm>
12 #include <vector>
13 #include <set>
14 #include <queue>
15 #include <cmath>
16 using namespace std;
17
18 const int MAXN = 1e6 + 10;
19 const int INF = 0x3f3f3f3f;
20 char s[110][110];
21 int in[110];
22 bool lin[27][27];
23 char ans[27];
24
25 bool TopoSort(void)
26 {
27 queue<int> q;
28 int cnt = 0;
29 for (int i=1; i<=26; ++i)
30 {
31 if (in[i] == 0)
32 {
33 q.push (i);
34 ans[cnt++] = 'a' + i - 1;
35 }
36 }
37 while (!q.empty ())
38 {
39 int now = q.front (); q.pop ();
40 for (int i=1; i<=26; ++i)
41 {
42 if (lin[now][i])
43 {
44 in[i]--;
45 if (in[i] == 0)
46 {
47 q.push (i);
48 ans[cnt++] = 'a' + i - 1;
49 }
50 }
51 }
52 }
53 ans[26] = '