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 si andti 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
题意:根据给出的演员表,输出其字典序的规则(即不一定按全abcd的字母表排),如果没有适合的字典表,输出Impossible。
题解:对给出的字符串取出其有序的字符进行拓扑排序,然后输出按照拓扑后的字母顺序,没有排序的字母,按照其正常位置输出。
注意:这里应该是正向的拓扑排序,上次做了一个反向的,现在我也搞不懂啥时候正向啥时候反向了,作孽啊。还有注意对aa、a这样的数据判断。
代码如下:
#include <stdio.h>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
#define idx(x) (x-'a')
int in[26];
bool vis[26];
vector<int> mapp[26];
vector<int> order; //表示顺序
void init()
{
for (int i = 0 ; i < 26 ; i++)
{
in[i] = -1;
vis[i] = false;
}
}
bool topo()
{
priority_queue<int,vector<int>,greater<int> > Q;
for (int i = 0 ; i < 26 ; i++)
{
if (in[i] == 0)
Q.push(i);
}
while (!Q.empty())
{
int pr = Q.top();
Q.pop();
in[pr] = -1;
order.push_back(pr);
for (int i = 0 ; i < mapp[pr].size() ; i++)
{
int ne = mapp[pr][i];
in[ne]--;
if (in[ne] == 0)
Q.push(ne);
}
}
for (int i = 0 ; i < 26 ; i++)
{
if (in[i] != -1) //有环
return true;
}
return false;
}
void Print()
{
// for (int i = 0 ; i < order.size() ; i++)
// printf ("%c\n",'a'+order[i]);
for (int i = 0 ; i < 26 ; i++)
{
if (!vis[i])
printf ("%c",'a'+i);
else
{
while (!order.empty() && *(order.begin()) <= i) //一直输出直到末尾是靠后的数
{
printf ("%c",'a' + (*(order.begin())));
order.erase(order.begin());
}
}
}
puts("");
}
int main()
{
int n;
bool flag = false; //是否长度问题排反,例如 aa 和 a
init();
scanf ("%d",&n);
char str[105][105];
scanf ("%s",str[0]);
if (n == 1)
{
puts ("abcdefghijklmnopqrstuvwxyz");
return 0;
}
for (int i = 1 ; i < n ; i++)
{
scanf ("%s",str[i]);
for (int j = 0 ; str[i-1][j] != '\0' ; j++) //这里要注意判断是否因长度问题排反
{
if (str[i][j] == '\0')
{
flag = true;
break;
}
if (str[i-1][j] != str[i][j])
{
mapp[idx(str[i-1][j])].push_back(idx(str[i][j])); //正向拓扑
if (in[idx(str[i-1][j])] == -1)
in[idx(str[i-1][j])] = 0;
if (in[idx(str[i][j])] == -1)
in[idx(str[i][j])] = 0;
in[idx(str[i][j])]++;
vis[idx(str[i-1][j])] = true;
vis[idx(str[i][j])] = true;
break;
}
}
}
if (flag || topo()) //排序并判断是否有环 (利用短路原理,如果flag为true则topo不进行)
puts ("Impossible");
else
Print();
return 0;
}