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. 再谈KMP

    昨天讲解了字典树和AC自动机后感觉整个人都蒙掉了.还好就是自己今天在网上看见一篇对KMP讲解非常详细的帖子,果断收藏.(点击这里查看) 然后代码的实现也就简单分析一些了,具体的知识点大家直接自己链接过 ...

  2. Boxes in a Line(移动盒子)

      You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to sim ...

  3. 认识ASP.NET MVC的5种AuthorizationFilter

    在总体介绍了筛选器及其提供机制(<深入探讨ASP.NET MVC的筛选器>)之后,我们按照执行的先后顺序对四种不同的筛选器进行单独介绍,首先来介绍最先执行的AuthorizationFil ...

  4. js获取url传递参数,js获取url?号后面的参数

    方法一.正则表达式 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + " ...

  5. groupbox 下的datagridview的列标题字体修改混乱

        groupbox 下的datagridview的列标题字体修改混乱

  6. ProgressDialog

    几个方法介绍: 1.setMax() 设置对话框中进度条的最大值. 2.setTile() 设置标题. 3.setProgressStyl() 设置对话框中进度条的样式.例如:环形和水平. 参数: P ...

  7. C#窗口实现最小化到系统托盘

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. sql 比较2个test字段的值

    可以用 CAST([TEXT字段]AS VARCHAR(MAX)),然后再比较

  9. md5 加密模板

    public class MD5Util { public static String getDigestedPassword(String password) throws NoSuchAlgori ...

  10. C# 实现屏幕键盘 (ScreenKeyboard)

    原文地址:http://www.cnblogs.com/youzai/archive/2008/05/19/1202732.html 要实现一个屏幕键盘,需要监听所有键盘事件,无论窗体是否被激活.因此 ...