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个字符串 现在要求你重新定义字典序 使得n个字符串的顺序为升序  输出新的26个字母的字典序
题解:对相邻的两个字符串 在第一个字符不同的位置的两个字符间连一条单向边 拓扑排序
 #pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define LL long long
#define mod 1000000007
using namespace std;
char a[][];
int g[][];
int in[];
queue<int> q;
int vis[];
int ans[];
int jishu=;
int n;
bool topsort(){
while(!q.empty())
q.pop();
for(int i=;i<=;i++){
if(in[i]==){
q.push(i);
vis[i]=;
}
}
jishu=;
int exm;
while(!q.empty()){
exm=q.front();
ans[jishu++]=exm;
q.pop();
for(int i=;i<=;i++){
if(g[exm][i]==){
if(--in[i]==&&vis[i]==){
vis[i]=;
q.push(i);
}
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%s",a[i]);
for(int i=;i<n-;i++)
{
int l1,l2;
l1=strlen(a[i]);
l2=strlen(a[i+]);
int p=;
while(p<min(l1,l2)&&a[i][p]==a[i+][p]) p++;
if(p==l1&&l1<l2) continue;
if(p==l2&&l2<l1) {
printf("Impossible\n");//**
return ;
}
if(g[a[i][p]-'a'][a[i+][p]-'a']) continue;
g[a[i][p]-'a'][a[i+][p]-'a']=;
in[a[i+][p]-'a']++;
}
topsort();
if(jishu==){
for(int i=;i<=;i++)
printf("%c",ans[i]+'a');
}
else
printf("Impossible\n");
return ;
}

Codeforces Round #290 (Div. 2) 拓扑排序的更多相关文章

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

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

  2. CodeForces Round #290 Div.2

    A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. #include <cstdio> int main() { //freopen("in.txt&quo ...

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

  4. Codeforces Round #290 (Div. 2) D. Fox And Jumping dp

    D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...

  5. Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...

  6. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  7. Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模

    E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

  9. Codeforces Round #290 (Div. 2) B (dfs)

    题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...

随机推荐

  1. 使用FFMPEG 压缩png图片 与tinypng压缩结果对比

    Tinypng  https://tinypng.com/ 一个在线png压缩工具 FFmpeg https://ffmpeg.org/download.html 原图 903 kb Tinypng压 ...

  2. Go文件右键编译

    辛辛苦苦写好了.go文件 发现编译还得敲命令才行,或许配置一个好用点的IDE环境可以解决 但是有时候实在不想开IDE 于是在右键添加了一个编译功能 首先保证go相关的环境变量配置正确 Windows ...

  3. Kubernetes探索学习004--深入Kubernetes的Pod

    深入研究学习Pod 首先需要认识到Pod才是Kubernetes项目中最小的编排单位原子单位,凡是涉及到调度,网络,存储层面的,基本上都是Pod级别的!官方是用这样的语言来描述的: A Pod is ...

  4. 教你用Python解决非平衡数据问题(附代码)

    本文为你分享数据挖掘中常见的非平衡数据的处理,内容涉及到非平衡数据的解决方案和原理,以及如何使用Python这个强大的工具实现平衡的转换. 后台回复“不平衡”获取数据及代码~ 前言 好久没有更新自己写 ...

  5. Sqlserver 每日订单半小时数据统计

    ) '订单数' FROM (SELECT CASE THEN ), create_at, ) ),DATEPART(hh, create_at))+':00:00') ELSE ), create_a ...

  6. 2018年第九届蓝桥杯【C++省赛B组】

    2标题:明码 汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛.16点阵的字库把每个汉字看成是16x16个像素信息.并把这些信息记录在字节中. 一个字节可以存储8位信息,用32个字节就 ...

  7. sprint2 (第八天)

    今天课多,没做什么功能.这个sprint定的目标比较高,要实现的功能较多,可能完成不了目标值.因为GitHub下载和上传很慢,经常失败,所以这几天都没有更新GitHub,功能明天早点实现然后上传到Gi ...

  8. 第三次作业---excel导入数据库及显示

    好吧首先承认这次作业失败了,而且我并不知道原因.另外,我也没有采用PowerDesigner 设计所需要的数据库,代码就用了全部的时间.感觉自己就像一个刚学会爬着走路的小孩去参加一百一十米跨栏,能不能 ...

  9. Do~Hamburger~

    在上一次的结对编程中,我的结对队友是 方俊杰 ,大家都称他为“JJ师兄”. 我们两个彼此在合作中发现错误并在合作中一起进步. First(汉堡上层面包):     JJ他的JAVA功底比我扎实很多,所 ...

  10. Mac OS10.10 openfire无法启动问题

    1.我用的Java版本是Version 8 Update 51,验证方法可到这个网址下去验证http://www.java.com/zh_CN/download/installed.jsp 2.ope ...