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. python中threading多线程

    python中有两个处理多线程的模块thread和threading.其中thread提供了多线程底层支持的模块,以低级原始的发那个是来处理和控制线程,使用起来较为复杂:而threading基于thr ...

  2. [Android]开源中国源码分析之一---启动界面

    开源中国android端版本号:2.4 启动界面: 在AndroidManifest.xml中找到程序的入口, <activity android:name=".AppStart&qu ...

  3. POJ 2431 贪心+优先队列

    题意:一辆卡车距离重点L,现有油量P,卡车每前行1米耗费油量1,途中有一些加油站,问最少在几个加油站加油可使卡车到达终点或到达不了终点.   思路:运用优先队列,将能走到的加油站的油量加入优先队列中, ...

  4. Linux 基本命令___0002

    来源:https://mp.weixin.qq.com/s/DmfpDfWpWRV3EDItDdYgXQ #配置vim #http://www.cnblogs.com/ma6174/archive/2 ...

  5. LeetCode——max-points-on-a-line

    Question Given n points on a 2D plane, find the maximum number of points that lie on the same straig ...

  6. Java编程思想 两个主函数

    //: initialization/DynamicArray.javapackage initialization; /* Added by Eclipse.py */// Array initia ...

  7. 0.00-050613_ZC_Chapter4_20151230

    1. 32位 保护模式 段选择符 --> 段描述符(段描述符表) --> 段基地址 + 偏移量  ==> 线性地址(ZC: 这个地址就是段的开始地址) 1.2. 段限长字段LIMIT ...

  8. Hibernate异常_01

    1. 在使用 Hibernate(ojdbc14.jar[1536554字节,Win7显示大小为1501KB]) 操作 Oracle10g(32位)的时候,出现如下 error: INFO: HHH0 ...

  9. gym强化学习入门demo——随机选取动作 其实有了这些动作和反馈值以后就可以用来训练DNN网络了

    # -*- coding: utf-8 -*- import gym import time env = gym.make('CartPole-v0') observation = env.reset ...

  10. Git学习--创建版本库

    什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...