Fox And Names
原题链接 http://codeforces.com/problemset/problem/510/C
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 si and ti according to their order in alphabet.
Input
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.
Output
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).
Examples
Input
3 rivest shamir adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10 tourist petr wjmzbmr yeputons vepifanov scottwu oooooooooooooooo subscriber rowdark tankengineer
Output
Impossible
Input
10 petr egor endagorion feferivan ilovetanyaromanova kostka dmitriyh maratsnowbear bredorjaguarturnik cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7 car care careful carefully becarefuldontforgetsomething otherwiseyouwillbehacked goodluck
Output
acbdefhijklmnogpqrstuvwxyz
找出这个字符串的字典序的顺序,如果没有就输出“Impossible”。拓扑排序,对于每个字符串都和它的下一个字符串进行比较,在判断每个字母的入度时要注意,例如字符串aaa和字符串a的大小关系,并且,如果前面的字母都一样,而前面的一个字符串长度大于后面的一个字符串,就不存在字典序,判定为错误。最后将把每一个字母的入度求出来,用拓扑排序进行排序;
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n;
char a[1001][1001];
int w[200];//存入每个字母的入度;
int mapp[200][200];//存储字母的字典序关系;
char p[1001];//存储拓扑排序结果;
void topo()
{
int i,j,t=0;
queue<int>q;
for(i=0; i<26; i++)//找出出现过的字母;
{
if(!w[i])
{
q.push(i);
p[t++]=i+'a';
}
}
while(!q.empty())//进行拓扑排序;
{
int k=q.front();
q.pop();
for(i=0; i<26; i++)
{
if(mapp[k][i]==1)
{
w[i]--;
if(w[i]==0)
{
q.push(i);
p[t++]=i+'a';
}
}
}
}
if(t<26)
{
printf("Impossible\n");
}
else
{
p[t]='\0';
printf("%s\n",p);
}
return ;
}
int main()
{
while(~scanf("%d",&n))
{
memset(w,0,sizeof w);
memset(mapp,0,sizeof mapp);
for(int i=0; i<n; i++)
{
scanf("%s",a[i]);
}
int flag1=0,flag2;
for(int i=0; i<n-1; i++)
{
flag2=0;
int l1=strlen(a[i]);
int l2=strlen(a[i+1]);
int l=min(l1,l2);
for(int j=0; j<l; j++)
{
if(a[i][j]!=a[i+1][j])
{
if(mapp[a[i][j]-'a'][a[i+1][j]-'a']==0)//标记关系
{
mapp[a[i][j]-'a'][a[i+1][j]-'a']=1;
w[a[i+1][j]-'a']++;//存储字母的入度;
}
flag2=1;//如果找到了一个大小关系,后面也便没有了比较准则;
break;
}
}
if(flag2==0&&l1>l2)//两个字符串没有找到不同的字母,并且后面一个字符串的长度大于前面的一个字符串便为错误;
{
flag1=1;
break;
}
}
if(flag1==1)
{
printf("Impossible\n");
}
else
{
topo();
}
}
return 0;
}