Codeforces Round #290 (Div. 2) 拓扑排序

时间:2022-11-28 18:45:29
C. Fox And Names
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 tiaccording 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

题意:给你n个字符串 现在要求你重新定义字典序 使得n个字符串的顺序为升序  输出新的26个字母的字典序
题解:对相邻的两个字符串 在第一个字符不同的位置的两个字符间连一条单向边 拓扑排序
 #pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define LL long long
#define mod 1000000007
using namespace std;
char a[][];
int g[][];
int in[];
queue<int> q;
int vis[];
int ans[];
int jishu=;
int n;
bool topsort(){
while(!q.empty())
q.pop();
for(int i=;i<=;i++){
if(in[i]==){
q.push(i);
vis[i]=;
}
}
jishu=;
int exm;
while(!q.empty()){
exm=q.front();
ans[jishu++]=exm;
q.pop();
for(int i=;i<=;i++){
if(g[exm][i]==){
if(--in[i]==&&vis[i]==){
vis[i]=;
q.push(i);
}
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%s",a[i]);
for(int i=;i<n-;i++)
{
int l1,l2;
l1=strlen(a[i]);
l2=strlen(a[i+]);
int p=;
while(p<min(l1,l2)&&a[i][p]==a[i+][p]) p++;
if(p==l1&&l1<l2) continue;
if(p==l2&&l2<l1) {
printf("Impossible\n");//**
return ;
}
if(g[a[i][p]-'a'][a[i+][p]-'a']) continue;
g[a[i][p]-'a'][a[i+][p]-'a']=;
in[a[i+][p]-'a']++;
}
topsort();
if(jishu==){
for(int i=;i<=;i++)
printf("%c",ans[i]+'a');
}
else
printf("Impossible\n");
return ;
}