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. Centos7 zabbix 分布式监控

    分布式监控 zabbix Server ===> zabbix agent (只能同一个局域网监控)     分布式监控:         a. 分担压力,降低负载         b. 多机房 ...

  2. python 原生态调用server服务————SimpleHTTPServer

    python 原生态调用server服务,接收http传递的参数并且处理返回结果 很多blog中都是抄了官网的,没有说明参数如何接收 1.代码中提供了post与get两种方式来发起请求,但是传递参数时 ...

  3. spring boot之创建web项目并访问jsp页面

    1,创建spring boot的web项目 刚创建好的项目路径如下: 2,pom中要有下面的依赖 <dependency> <groupId>org.springframewo ...

  4. WebGL学习笔记四点一

    上一章是对图形的变换,这一章的第一节主要介绍了光栅化的过程,在创建多个颜色的三角形的过程中顶点着点器的过程如下 ,1.首先通过attribute的变量从javascript中获取数据,根据drawAr ...

  5. Merge join、Hash join、Nested loop join对比分析

    简介 我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge Join,Hash Join ...

  6. AVMoviePlayer 视频播放器

    AVMoviePlayer 是使用系统框架 MPMoviePlayerController 封装的视频播放器 一.功能: 1.根据手机旋转自由切换横竖屏:2.手势轻点显示/隐藏topView/bott ...

  7. 复利计算测试(C语言)

    对我们和复利计算程序,写单元测试. 有哪些场景? 期待的返回值 写测试程序. 运行测试. 测试模块 测试输入 预期结果 运行结果 bug跟踪 计算终值 (本金,年限,利率) 终值     1 (100 ...

  8. 第九周(11.11-11.17)----Beta版本视频发布

    beta阶段视频发布地址: http://v.youku.com/v_show/id_XMTgxNjE2NzY3Mg==.html

  9. [转]让opencv输出人脸检测的得分(置信率)

    转自:http://www.cnblogs.com/sciencefans/ 作者:sciencefans 最近项目略多,其中一个需要找出一些和脸比较像但是不是脸的负样本,想用opencv的人脸检测器 ...

  10. 将博客搬至CSDN和和自己的网站

    将博客同步一份到CSDN去, CSDN博客地址:https://blog.csdn.net/klkfl ---------------- 分割线 2018-10-7 自己用typecho 搭建了一个博 ...