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

Examples
input

Copy
3
rivest
shamir
adleman
output

Copy
bcdefghijklmnopqrsatuvwxyz
input

Copy
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
output

Copy
Impossible
input

Copy
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
output

Copy
aghjlnopefikdmbcqrstuvwxyz
input

Copy
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
output

Copy
acbdefhijklmnogpqrstuvwxyz

大坑点:
2
aa
a
这应该是impossible 题意:有n个按照某一字母表排好序的字符串,问是否存在这样的字母表。或者说是否存在这样一张字母表使字符串排序后是这个样子。

思路:可以根据字符串的顺序得出一些图的指向,比如abc和abd,可以得到c->d,这样就可以得到若干边,根据这个进行拓扑排序即可。注意有个陷阱是如果存在xy>x,那就无解。

 
#include<bits/stdc++.h>
using namespace std;
#define ll long long

char a[][];
int in[];
int out[];
bool book[];
vector<int>v[];
queue<int>q;
int main()
{
int n;scanf("%d",&n);
memset(book,,sizeof(book));
memset(in,,sizeof());
memset(out,,sizeof());
while(!q.empty()) q.pop();
for(int i=;i<=n;i++)scanf("%s",&a[i]);
for(int i=;i<=n;i++)
{ for(int j=i+;j<=n;j++)
{
int l=min(strlen(a[i]),strlen(a[j]));
for(int k=;k<l;k++)
{
if(a[i][k]!=a[j][k])
{
in[a[j][k]-'a'+]++;//保存入度
out[a[i][k]-'a'+]++;//其实不用考虑出度的
v[a[i][k]-'a'+].push_back(a[j][k]-'a'+);
break;
}
if(k==l-&&strlen(a[i])>strlen(a[j]))
{
printf("Impossible");
return ;
}
}
}
}
//cout<<in['e'-'a'+1]<<endl;
// cout<<in['p'-'a'+1]<<endl;
int k=;
while()
{
int x=-;
for(int i=;i<=;i++)
{
if(in[i]==&&!book[i])//找到没有入度的点
{
x=i;
break;
}
}
if(x==-&&k<)
{
//cout<<k<<endl;
cout<<"Impossible";
break;
}
if(x==-) break;
k++;
book[x]=;
q.push(x);
for(int i=;i<v[x].size();i++)//将挑选出来的改点的边都去掉
{
in[v[x][i]]--;
}
}
if(k==)
{
while(!q.empty())
{
char t=q.front()+'a'-;
cout<<t;
q.pop();
}
}
return ;
}

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

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

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

  2. CF Fox And Names (拓扑排序)

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

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

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

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

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

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

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

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

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

  7. Codeforces 510C (拓扑排序)

    原题:http://codeforces.com/problemset/problem/510/C C. Fox And Names time limit per test:2 seconds mem ...

  8. 【codeforces 510C】Fox And Names

    [题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...

  9. C. Fox And Names

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

随机推荐

  1. 斯坦福机器学习视频笔记 Week8 无监督学习:聚类与数据降维 Clusting & Dimensionality Reduction

    监督学习算法需要标记的样本(x,y),但是无监督学习算法只需要input(x). 您将了解聚类 - 用于市场分割,文本摘要,以及许多其他应用程序. Principal Components Analy ...

  2. php flock 使用实例

    php flock 使用实例 bool flock ( resource $handle , int $operation [, int &$wouldblock ] ) flock()允许执 ...

  3. Why does typeof array with objects return “Object” and not “Array”?

    https://stackoverflow.com/questions/4775722/check-if-object-is-an-array One of the weird behaviour a ...

  4. 泛型学习第四天——List泛型终结:什么是List泛型,泛型筛选,泛型排序

    为什么要用泛型集合? 在C# 2.0之前,主要可以通过两种方式实现集合: a.使用ArrayList 直接将对象放入ArrayList,操作直观,但由于集合中的项是Object类型,因此每次使用都必须 ...

  5. spark学习1(hadoop集群搭建)

    把原先搭建的集群环境给删除了,自己重新搭建了一次,将笔记整理在这里,方便自己以后查看 第一步:安装主节点spark1 第一个节点:centos虚拟机安装,全名spark1,用户名hadoop,密码12 ...

  6. UOJ104 【APIO2014】Split the sequence

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. read_excel

    read_excel默认把第一行作为各个列名, 用headers=None,读取表时,可以让第一行不为列名. 而不是names,col之类的参数

  8. Aix-Linux查看系统信息

    1.查看内存.磁盘等使用情况 命令:nmon 输入命令nmon之后,按M键memory,N键NetWork,J键查看目录使用情况. 2.查看防火墙 命令:smit 输入之后按照图形化操作 Commun ...

  9. review35

    使用Thread创建线程通常使用的方法是:Thread(Runnable target).该构造方法中的参数是一个Runnable类型的接口,因此在创建线程对象时必须向构造方法的参数传递一个实现Run ...

  10. 论文笔记 — L2-Net: Deep Learning of Discriminative Patch Descriptor in Euclidean Space

    论文: 本文主要贡献: 1.提出了一种新的采样策略,使网络在少数的epoch迭代中,接触百万量级的训练样本: 2.基于局部图像块匹配问题,强调度量描述子的相对距离: 3.在中间特征图上加入额外的监督: ...