C. Fox And Names

题目连接:

http://codeforces.com/contest/510/problem/C

Description

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

Sample Input

3

rivest

shamir

adleman

Sample Output

bcdefghijklmnopqrsatuvwxyz

Hint

题意

给你n个串,然后让你输出一个字符串,使得根据这个字符串的先后顺序排序的n个串

和给你的顺序是一样的

题解:

每两个串相比较,只需要一对字符不一样

记录一下是哪一对,然后再dfs一波就好了

注意坑点:

有可能两个串只有长度不一样

形成环

代码

#include<bits/stdc++.h>
using namespace std; string s[120];
vector<int> G[30];
int flag = 0;
int ran[40];
int vis[120],used[120];
int tot = 0;
void solve(int x)
{
for(int i=0;i<s[x].size()&&i<s[x+1].size();i++)
{
if(s[x][i]!=s[x+1][i])
{
G[s[x+1][i]-'a'].push_back(s[x][i]-'a');
return;
}
}
if(s[x+1].size()<s[x].size())
{
puts("Impossible");
exit(0);
}
}
void dfs(int x)
{
vis[x]=used[x]=1;
for(int i=0;i<G[x].size();i++)
{
if(used[G[x][i]])
{
puts("Impossible");
exit(0);
}
if(!vis[G[x][i]])
dfs(G[x][i]);
}
used[x]=0;
ran[tot++]=x;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
cin>>s[i];
for(int i=0;i<n-1;i++)
solve(i);
for(int i=0;i<26;i++)
{
memset(used,0,sizeof(used));
if(!vis[i])
dfs(i);
}
for(int i=0;i<26;i++)
printf("%c",ran[i]+'a');
}

Codeforces Round #290 (Div. 2) C. Fox And Names dfs的更多相关文章

  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) D. Fox And Jumping dp

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

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

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

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

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

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

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

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...

  8. 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake

    题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...

  9. CodeForces Round #290 Div.2

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

随机推荐

  1. 【转载】JMeter3.0图形化HTML报告中文乱码问题处理

    由于个人在JMeter 3.0的实际应用中,脚本中的Test Plan/Sampler等元件命名都没有使用中文,所以在之前介绍Dashboard Report特性的博客(原文戳这里))成文时,没有提到 ...

  2. UBUNTU上的GIT SERVER

    Git是一个开源的版本控制系统,由Linus Torvalds主导,用于支持Linux内核开发.每一个Git工作目录,都是一个完整的代码库,包含所有的提交历史.有能力跟踪所有的代码版本,而不会去依赖于 ...

  3. ofbiz进阶之框架配置文件指导

    The Open For Business Project: Framework Configuration Guide 原文链接:http://ofbiz.apache.org/docs/corec ...

  4. ./filezilla: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory

    opensuse系统 在filezilla官网下载压缩文件解压运行后报 ./filezilla: error while loading shared libraries: libpng12.so.0 ...

  5. 学习笔记之--MySQL图形界面软件Navicat Premium的安装

    最近因项目开发需要,搁置已久的MySQL再次用到.由于以前都是使用命令行进行操作的,没有图形界面.经同学介绍,安装了一个MySQL的图形界面软件.各种数据库的操作也变得直观方便了很多.现在记录下来,一 ...

  6. ruby on rails出现的问题ActiveModel::ForbiddenAttributesError

    首先分清楚我们在搞rails时.看资料和所使用的环境的版本号是否同样.看的资料是rails3.2,电脑配置的环境是4.0,就会出现这样的安全防范措施的问题. 这类问题大多出如今new或者create两 ...

  7. layer.msg 添加在Ajax之前 显示进度条。

    一.使用方法:1)必须先引入jQuery1.8或以上版本 <script src="jQuery的路径"></script> <script src= ...

  8. 修改Oracle【12C】字符集

    select userenv('language') from dual; //查看系统字符集编码 select * from nls_database_parameters where parame ...

  9. MATLAB 曲线形状,粗细,颜色使用大全

    通过改变R-G-B 的值改变线条的颜色: $$\tt\Large plot(x,y,'Color',[R~~G~~B]);$$ 通过改变$c\in[1,+\infty)$的值改变线条的粗细 $$\tt ...

  10. c++11新标准for循环和lambda表达式

    :first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...