C. Fox And Names
time limit per test
memory limit per test
input
output
lexicographical
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!
lexicographical
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 tiaccording to their order in alphabet.
Input
n (1 ≤ n ≤ 100): number of names.
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).
Sample test(s)
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
对于相邻2个字符串两两比较字典序,得到字符拓扑关系,排序。
注意重边indegree不要再加
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (500+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n;
char s[MAXN][MAXN]={0};
bool f[MAXN][MAXN]={0};
int indegree[MAXN]={0};
int q[MAXN*2]={0};
bool b[MAXN]={0};
void topsort()
{
int head_=1,tail=0;
Fork(i,'a','z')
if (indegree[i]==0)
{
q[++tail]=i;b[i]=1;
}
while (head_<=tail)
{
int now=q[head_];
Fork(v,'a','z')
if (f[now][v])
{
indegree[v]--;
if (indegree[v]==0)
{
q[++tail]=v;b[v]=1;
}
}
head_++;
}
if (tail==26)
For(i,26) cout<<(char)q[i];
else cout<<"Impossible";
cout<<endl;
}
int main()
{
// freopen("Names.in","r",stdin);
// freopen(".out","w",stdout);
cin>>n;
For(i,n) scanf("%s",s[i]);
For(i,n-1) //确定比较关系
{
int l1=strlen(s[i]),l2=strlen(s[i+1]);
int p=0;
while (p<min(l1,l2)&&s[i][p]==s[i+1][p]) p++;
if (s[i][p]==s[i+1][p]) continue;
if (p==l1&&l1<l2) continue;
if (p==l2&&l2<l1)
{
cout<<"Impossible"<<endl;
return 0;
}
if (f[s[i][p]][s[i+1][p]]) continue;
f[s[i][p]][s[i+1][p]]=1;
indegree[s[i+1][p]]++;
}
topsort();
return 0;
}