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. GridControl表头全选操作实现之最优方法

    突然发现很久没有写博客了. 昨天整了个Windows Live Writer 就为了以后好好写写博客. 所以,开始咯. 为了积累,也为了分享. 之前在博客园中看到一篇文章:<Winform分页控 ...

  2. Java封装 properties文件操作

    /** * Prop. Prop can load properties file from CLASSPATH or File object. */ public class Prop { priv ...

  3. Codeforces Round #219 (Div. 1)(完全)

    戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...

  4. Windows 下如何设置 只允许固定IP远程访问

    通过设置IP安全策略限制固定IP访问 说明: (1)以XP环境为例,步骤:先禁止所有IP,再允许固定IP访问. (2)配置过程中很多步骤图是重复的,一些没价值的图就省略了: (3)光看的话可能中间重复 ...

  5. 开源的读取Excel文件组件-ExcelDataReader

    ExcelDataReader可以读取 Microsoft Excel 文件 ('97-2007),支持Windows  .Net Framework 2 +. Windows Mobile with ...

  6. 一个 C# 获取高精度时间类(调用API QueryP*)

    如果你觉得用 DotNet 自带的 DateTime 获取的时间精度不够,解决的方法是通过调用 QueryPerformanceFrequency 和 QueryPerformanceCounter这 ...

  7. java的BigDecimal

    java的BigDecimal 一般设计到高精度的加法或乘法或者阶乘的求和积都会用到BigDecimal这个类. import java.util.*;import java.math.BigDeci ...

  8. SQL拆分多规则的字符串分离数字。

    --拆分多规则字符串 DECLARE @Codes NVARCHAR(MAX) SET @Codes = '6*BC-007,*BC-016,9*BC-015' --对于*BC-015这种情况,则Qt ...

  9. MEF 编程指南(九):部件生命周期

    理解 MEF 容器部件生命周期和实现是非常重要的事情.考虑到 MEF 关注可扩展应用程序.这变得尤为重要.生命期可以解释为期望部件的共享性(transitively, its exports)   共 ...

  10. 【转】Ruby入门教程(一)

    1. Ruby环境搭建 在Windows下,搭建Ruby环境,比较简单的方法是在“RubyInstaller”上下载一个合适的版本(D瓜哥使用的是最新版),直接安装就可以了. 另外,吐槽两句,网上有人 ...