Description

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).

Sample Input

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
标准的拓扑排序。
 #include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
char s[][];
queue<int> q;
int ans[],in[];
int g[][];
int cnt;
void toposort(){
queue<int> q;
for(int i = ; i<=; i++)
if(!in[i]) q.push(i);
cnt = ;
while(!q.empty()){
int tmp = q.front();
q.pop();
ans[++cnt] = tmp;
for(int j = ; j<=; j++){
if(g[tmp][j]){
in[j]--;
if(!in[j]) q.push(j);
}
}
}
}
void input(){
int n;
scanf("%d",&n);
for(int i = ; i<=n; i++) scanf("%s",s[i]);
bool flag1 = true;
for(int i = ; i<n; i++){
int lena = strlen(s[i]);
int lenb = strlen(s[i+]);
char a,b;
int tem1,tem2;
int flag = ;
for(int j = ; j<min(lena,lenb); j++){
if(s[i][j]!=s[i+][j]){
a = s[i][j];
b = s[i+][j];
tem1 = a - 'a' + ;
tem2 = b - 'a' + ;
if(!g[tem1][tem2]){ // 这没考虑到已经有路径的条件,一直WA
g[tem1][tem2] = ;
in[tem2]++;
}
flag = ;
break;
}
}
if(!flag&&lena>lenb){
flag1 = false;
break;
}
}
if(!flag1) printf("Impossible\n");
else{
toposort();
if(cnt<) {
printf("Impossible\n");
return;
}
for(int i = ; i<=; i++){
printf("%c",ans[i]-+'a');
}
printf("\n");
}
}
int main()
{
input();
return ;
}/*
26
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
*/

卷珠帘

Fox And Names的更多相关文章

  1. Codeforces Round #290 (Div. 2) C. Fox And Names dfs

    C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...

  2. C. Fox And Names

    C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. CF Fox And Names (拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. codeforce 510C Fox And Names(拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. (CodeForces 510C) Fox And Names 拓扑排序

    题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...

  6. [CF #290-C] Fox And Names (拓扑排序)

    题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...

  7. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

  8. codeforces 510 C Fox And Names【拓扑排序】

    题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入 ...

  9. CodeForces 510C Fox And Names (拓扑排序)

    <题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...

随机推荐

  1. springmvc权限拦截器

    配置文件spring-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  2. wpa_supplicant wpa_cli 的使用说明

    wpa_supplicant -d -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf -iwlan0 -B   会在/data/misc/wifi/下产生 ...

  3. namenode ha

    http://blog.csdn.net/totxian/article/details/45248399 http://www.aboutyun.com/thread-13679-1-1.html ...

  4. adodb.stream对象的方法/属性

    Cancel 方法      使用方法如下      Object.Cancel      说明:取消执行挂起的异步 Execute 或 Open 方法的调用.Close   方法      使用方法 ...

  5. HTML、XHTML和HTML5区别与联系

    1.XHTML与HTML最大的区别: ① XHTML标签名必须小写(错误:<Div> 正确:<div>) ② XHTML元素必须被关闭(错误:<p>  正确:< ...

  6. mysql具体语句示例

    建表:(not null ,auto_increment, unique , primary key) create database balfish;use balfish;create table ...

  7. Spotlights

    Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  8. boost库之graph入门

    #include <boost/graph/undirected_graph.hpp> #include <boost/graph/adjacency_list.hpp> us ...

  9. linux的学习系列 4---文件权限和访问模式

    为了更加安全的存储文件,Linux为不同的文件赋予了不同的权限,每个文件都拥有下面三种权限: 所有者权限:文件所有者能够进行的操作 组权限:文件所属用户组能够进行的操作 外部权限(其他权限):其他用户 ...

  10. python 函数部分

    #初始化 def init(data): data['first']={} data['middle']={} data['last']={} #查看条件 def lookup(data,label, ...