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 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: $s_i \neq t_i$. 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 $s_i$ and $t_i$ according to their order in alphabet.

题意概述

给定$n$个字符串$name_1, name_2, name_3, \ldots, name_n$,要求你给出一张字母表,使得这些字符串是按照你所给出字母表的字典序从小到大排序的。

数据范围:$1 \le n \le 100, \ 1 \le |name_i| \le 100$。

算法分析

很明显,对于任意两个字符串$name_i, name_j \ (i \lt j)$来说,设其第一个不同字母的位置为$p$,则有$index_{name_{i, p}} \lt index_{name_{j, p}}$,其中$index_i$表示字母$i$在字母表中的位置。可以发现,这构成了一张拓扑图,进行一次拓扑排序就能解决问题。

注意到其中有些字符串是另一些字符串的前缀。如果$name_i$是$name_j$的前缀($name_i \neq name_j$),且$i \gt j$,则直接输出 Impossible。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct author {
int id;
string name;
} a[101];
bool cmp(author a, author b) {
return a.name < b.name;
}
int n, in[26];
bool graph[26][26];
queue<int> que;
string ans;
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].name;
a[i].id = i;
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i < n; ++i) {
for (int j = i + 1; j <= n; ++j) {
int p = 0, len1 = a[i].name.length(), len2 = a[j].name.length();
while (p < len1 && p < len2 && a[i].name[p] == a[j].name[p]) ++p;
if (p == len1 && p < len2 && a[i].id > a[j].id) {
cout << "Impossible" << endl;
return 0;
}
if (p < len1) {
if (a[i].id < a[j].id) graph[a[i].name[p] - 'a'][a[j].name[p] - 'a'] = true;
else graph[a[j].name[p] - 'a'][a[i].name[p] - 'a'] = true;
}
}
}
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < 26; ++j) {
if (graph[i][j]) ++in[j];
}
}
for (int i = 0; i < 26; ++i) {
if (!in[i]) que.push(i);
}
while (!que.empty()) {
int t = que.front();
que.pop();
for (int i = 0; i < 26; ++i) {
if (graph[t][i]) {
--in[i];
if (!in[i]) que.push(i);
}
}
ans += t + 'a';
}
if (ans.length() < 26) {
cout << "Impossible" << endl;
} else {
cout << ans << endl;
}
return 0;
}

Fox and Names
https://regmsif.cf/2017/06/24/oi/fox-and-names/
作者
RegMs If
发布于
2017年6月24日
许可协议