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个字符串(全小写),让你按照输入的顺序来个字母表排列,(就是改变字母表的某些字母位置,使得你的输入是按照新的字典序)

    如果前一个是后一个的子串,那么前一个一定小于后一个所以可以跳过。反之,如果后一个是前一个的子串,无论字典序怎么改都无法成立就输出"Impossible"。

    新的字典序就是按照2个字符串的不同首字母,构建的有向图。

    在使用拓扑排序就可以了。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define mset(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
string s[maxn];
int gap[][];
int c[], topo[], t;
bool dfs(int u)
{
c[u] = -;
for(int v = ; v<; v++) if(gap[u][v]){
if(c[v] < ) return false;
else if(!c[v] && !dfs(v)) return false;
}
c[u] = ; topo[--t] = u;
return true;
}
bool toposort()
{
t = ;
mset(c, );
for(int i=;i<;i++) if(!c[i])
if(!dfs(i)) return false;
return true;
}
int main()
{
int n;
cin >> n;
for(int i=;i<n;i++) cin >> s[i];
for(int i=;i<n-;i++){
int len1 = s[i].size();
int len2 = s[i+].size();
int p = ;
while(p<len1 && p<len2 && s[i][p]==s[i+][p]) p++;
if(p == len1 && len1 < len2) continue;
if(p == len2 && len2 < len1) {cout<<"Impossible"<<endl;return ;}
if(gap[s[i][p]-'a'][s[i+][p] -'a'] == ) continue;
gap[s[i][p]-'a'][s[i+][p]-'a']=;
}
if(toposort()){
for(int i=;i<;i++)
printf("%c", topo[i]+'a');
}
else
cout <<"Impossible"<< endl;
return ;
}

Codeforces 510C (拓扑排序)的更多相关文章

  1. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

  2. CodeForces - 721C 拓扑排序+dp

    题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c     //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...

  3. Codeforces 1100E 拓扑排序

    题意及思路:https://blog.csdn.net/mitsuha_/article/details/86482347 如果一条边(u, v),v的拓扑序小于u, 那么(u, v)会形成环,要反向 ...

  4. National Property CodeForces - 875C (拓扑排序)

    大意: n个字符串, 每次操作选出一种字符全修改为大写, 求判断能否使n个字符串字典序非降. 建源点s, 汇点t, s与所有必须转大写的连边, 必须不转大写的与t连边. #include <io ...

  5. Codeforces 1159E 拓扑排序

    题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html 代码: #include <bits/stdc++.h> #define LL ...

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

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

  7. Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序

    B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...

  8. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

    C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

  9. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

随机推荐

  1. oracle中not in 和 in的代替用法

    -- not in 的替代写法select col from table1 where col not in(select col from table2); select col,table2.co ...

  2. golang的数据类型之整型类型

    数据类型: 整数 : int, int32, int64, uint, uint32, uint64 字符串 : string 布尔:bool 浮点:float32 float64 uint 表示无符 ...

  3. Runnable、Callable、Future、FutureTask的区别

    转自:https://blog.csdn.net/jdsjlzx/article/details/52912701 FutureTask既是Future.Runnable,又是包装了Callable( ...

  4. spring-第五篇之spring容器中的bean

    1.bean的基本定义和bean别名 2.容器中bean的作用域 singleton:单例模式,在整个spring IoC容器中,singleton作用域的bean将只生成一个实例. prototyp ...

  5. quartz CronExpression

    一.Quartz Cron 表达式支持到七个域 名称 是否必须 允许值 特殊字符 秒 是 0-59 , - * / 分 是 0-59 , - * / 时 是 0-23 , - * / 日 是 1-31 ...

  6. Python入门习题1.温度转换

    这一节的课堂例题为: 例1.编写一个Python程序,完成摄氏度到华氏度,华氏度到摄氏度的温度转换. 解: (1)分析问题:利用程序实现温度转换,由用户输入温度值,程序给出输出结果. (2)划分边界: ...

  7. [SOL] #148. 数字格子问题

    说实话这题确实挺菜的... 废话少说,直接上代码^O^ Code: #include <bits/stdc++.h> using namespace std; inline int rea ...

  8. 【接口工具】接口抓包工具之Fiddler

    一.Fiddler的基本功能介绍 Fiddler的官方网站: https://www.telerik.com/purchase/fiddler Fiddler是最强大最好用的Web调试工具之一,它能记 ...

  9. Java集合、IO流、线程知识

    一.集合: 1. 集合框架: 1)Collection (1)List:有序的,有索引,元素可重复. (add(index, element).add(index, Collection).remov ...

  10. JSONP面试

    jQuery 的 JSONP的原理是动态创建一个 script 标签,利用src 发送请求,获取数据 回调函数的键名叫做  callback 跟ajax没有关系 JSONP:主要是利用 script标 ...