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 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的更多相关文章
- 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names
题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...
- DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots
题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...
- 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake
题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...
- CodeForces Round #290 Div.2
A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. #include <cstdio> int main() { //freopen("in.txt&quo ...
随机推荐
- Provider 错误 '80004005' 未指定的错误 的最终解决方法
今天在配置公司的香港WEB服务器Server2003系统,建好应用程序池后,发现远行程序经常出现下面的错误,刷新几下又可以,但过不了多久又是出现下面的错误!! 在网上查找相关问题得知,这是2003SP ...
- 建立连接ALM的xml config文件
我就不贴所有的了,如果有谁想要所有源码和应用程序,可以密我 这里我贴下如何在第一次运行的时候自动建立一个ALMConfig的xml文件 private static void CreateALMCon ...
- python开源包提交到pypi社区
为啥要提交到pypi?因为提交成功后,你今后想用你自己写的模块,只要pip install一下就可以了. 那么如何提交?请参看本篇教程 首先要确定你的包叫啥名,比如我的包叫xlutils3,既然确定了 ...
- 数往知来 asp.net 聊天室问题解决方案<十六>
1:在服务端创建了一个负责监听的sokcet //三个:采用TCP协议. ListenSocket = new Socket(AddressFamily.InterN ...
- Visual Basic相关图书推荐
Visual Basic从入门到精通(第2版) 作 者 国家863中部软件孵化器 编 出 版 社 人民邮电出版社 出版时间 2015-03-01 版 次 2 页 数 61 ...
- 30+简约时尚的Macbook贴花
当Macbooks Pro电脑在他们的设计之下仍然漂亮.独一无二时,我想说,他们已经成为相当的主流了.有时候如果你回忆过去的很美好的日子,当人们偷偷欣赏你的技术装备 的时候,大概是为你的外表增加亮点的 ...
- html5_common.js
(function(){ this.sendAjax = function(url,func,formData,type){ type = type || "POST"; //默认 ...
- hadoop2.2伪分布安装加2.2源码编译
配置linux基本环境: --> java.ip.hostname.hosts.iptables.chkconfig.ssh环境配置 hadoop2.2安装在linux64位机器上,需要对源码进 ...
- 3Com Network Supervisor与IBM Tivoli NetView两款网管软件操作视频
3Com Network Supervisor与IBM Tivoli NetView两款网管软件操作视频 网管软件必须能够实实在在的给我们带来好处,对于企业网络管理来说,其作用体现在以下几个方面: ...
- 2010“架构师接龙”问答--杨卫华VS赵劼(转)
add by zhj:虽然是几年前的文章,但还是很有参考价值的 原文:http://blog.zhaojie.me/2010/05/programmer-magazine-2010-5-archite ...