题目:
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 siand ti according to their order in alphabet.
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.
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).
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
给n个字符串,让你确定一个a-z的字典序(重新规定先后关系)使得这n个串是按照这个字典序给出的。
分析:
给出一个序列 -> 拓扑排序
字符串前后两个进行比较来建图,不需要两两比较。找到第i个和第i+1个串第一个不相同的字母,前者向后者连一条边。若第i+1个串是第i个串的子串,这两个串肯定不符合字典序
拓扑排序思想:
对于一个DAG(有向无环图),拓扑排序的结果是将所有节点排成一个线性序列。
算法:
(1)将有向图中入度为0的节点放到序列的最前面(同时入队),这些节点之间没有顺序.
(2)从图中删除这些顶点连出的所有边,及所连向的节点入度减一.
(3)对于(2)得到的新图,重复执行(1) (2),直至队列为空.
代码:
#include<algorithm>
#include<string>
#include<iostream>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 200;
vector<int> g[26];
string now,pre;
int in[26],ans[26];
int tot=0;
void toposort() {
queue<int> que;
for(int i=0; i<26; i++) if(!in[i]) que.push(i);
while(!que.empty()) {
int x = que.front();
ans[tot++] = x;
que.pop();
for(int i=0; i<g[x].size(); i++) {
in[g[x][i]]--;
if(!in[g[x][i]]) que.push(g[x][i]);
}
}
if(tot != 26) cout << "Impossible" << endl;
else {
for(int i=0; i<26; i++) {
cout <<char('a'+ ans[i]);
}
cout << endl;
}
}
int main(){
int n;
cin >> n;
for(int i=0; i<n; i++) {
cin >> now;
int len = min(now.length(),pre.length());
bool ok = false;
for(int j=0; j<len; j++) {
if(now[j] != pre[j]) {
ok = true;
g[pre[j]-'a'].push_back(now[j]-'a');
in[now[j]-'a']++;
break;
}
}
if(!ok&&now.length() < pre.length()) {
cout << "Impossible" << endl;
return 0;
}
pre=now;
}
toposort();
return 0;
}