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 inlexicographical 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 siand 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 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

拓扑排序第一题,注意特判一下下面的串是上面的串的前缀这种情况,直接输出Impossible了。

 #include <bits/stdc++.h>
using namespace std; bool G[][];
int IN[];
int ANS[]; void toposort(void);
int main(void)
{
int n;
char name[][];
bool flag; cin >> n;
for(int i = ;i < n;i ++)
cin >> name[i];
for(int i = ;i < n - ;i ++)
{
for(int j = ;name[i][j] && name[i + ][j];j ++)
{
flag = false;
if(name[i][j] != name[i + ][j])
{
if(!G[name[i][j] - 'a'][name[i + ][j] - 'a'])
{
G[name[i][j] - 'a'][name[i + ][j] - 'a'] = true;
IN[name[i + ][j] - 'a'] ++;
}
flag = true;
break;
}
}
if(!flag && strlen(name[i]) > strlen(name[i + ]))
{
puts("Impossible");
return ;
}
}
toposort(); return ;
} void toposort(void)
{
queue<int> que;
int sum = ;
int k = ;
int front; for(int i = ;i < ;i ++)
if(!IN[i])
{
ANS[k ++] = i;
sum ++;
que.push(i);
} while(!que.empty())
{
front = que.front();
que.pop();
for(int i = ;i < ;i ++)
if(G[front][i])
{
IN[i] --;
if(!IN[i])
{
ANS[k ++] = i;
que.push(i);
sum ++;
}
}
}
if(sum < )
puts("Impossible");
else
{
for(int i = ;i < ;i ++)
printf("%c",ANS[i] + 'a');
puts("");
} return ;
}

CF Fox And Names (拓扑排序)的更多相关文章

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

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

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

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

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

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

  4. CF510C Fox And Names——拓扑排序练习

    省委代码: #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> # ...

  5. CF 274D Lovely Matrix 拓扑排序,缩点 难度:2

    http://codeforces.com/problemset/problem/274/D 这道题解题思路: 对每一行统计,以小值列作为弧尾,大值列作为弧头,(-1除外,不连弧),对得到的图做拓扑排 ...

  6. CF 213A Game(拓扑排序)

    传送门 Description Furik and Rubik love playing computer games. Furik has recently found a new game tha ...

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

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

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

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

  9. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

随机推荐

  1. append some buttons to the standard datagrid pager bar

    <script type="text/javascript">  $(function(){   var pager = $('#dg').datagrid('getP ...

  2. 转】Apache解决高并发和高可用

    原博主于: http://www.ha97.com/5803.html   感谢! 服务器集群 Apache 和 nginx(web服务器) 1.  多台集群机器联合处理一个任务. 2.  一台机器处 ...

  3. awk的二维数组

    awk是不支持二维数组的,它的底层是一维数组,将两个key拼接为一维数组的key. 如下是其初始化和遍历 awk 'BEGIN{ for(i=0;i< 3; ++i) for(j = 0; j ...

  4. 如何在不装ORACLE的情况下使用PLSQL

    原来我电脑装了oracle跟plsql,然后使用plsql的.后来因为某些原因,我重装了系统,把装的软件都格调了,需要重新装.当时在装plsql的时候我就想,我一直都是直接用plsql远程连接的服务器 ...

  5. TypeScript学习笔记(五):接口

    使用接口 在前面的笔记中我们知道可以使用Object Type来指定参数的属性,如下: function printLabel(labelledObj: {label: string}) { cons ...

  6. word2010 ctrl v not work

    终于解决了word 2010中ctrl v 不能用的问题. 0 word ctrl c 可以用,右键粘贴可以正常使用,快捷键ctrl v不能用. 1 在excel中ctrl c 和ctrl v,可以正 ...

  7. 解决VS2012新建MVC4等项目时,收到加载程序集“NuGet.VisualStudio.Interop…”的错误

    初装V2012,新建MVC4新项目时出现以下错误: 解决方法为: 通过VS2012的“工具-扩展和更新-联机”安装“NuGet Package Manager”扩展包,可以顺利新建MVC4项目啦!

  8. C语言经典算法100例(二)

    11.判断某一年是否是闰年. //判断某一年份是否是闰年 int IsLeapYear(int year) { return (year % 400 == 0 || (year % 4 == 0) & ...

  9. [Javascript] Functor law

    Functor laws: 1. Identity: map(id) == id 2. Composition: compose(map(f), map(g)) == map(compose(f,g) ...

  10. [AngularJS] ngAnimate angular way !!

    Idea is set up javascript  as an api, then just change html to control the behavor. var app = angula ...