Codeforces Round #290 (Div. 2) C. Fox And Names dfs
2015-12-25 22:36
519 查看
C. Fox And Names
题目连接:
http://codeforces.com/contest/510/problem/CDescription
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
3rivest
shamir
adleman
Sample Output
bcdefghijklmnopqrsatuvwxyzHint
题意
给你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'); }
相关文章推荐
- 20151225--easyUI
- 新手C#协变和抗变学习总结
- 监督学习之生成学习算法——Andrew Ng机器学习笔记(四)
- Codis 是一个分布式 Redis 解决方案 推荐
- 从头开始swift2.0 仿乐乐医项目(二)列表的填充
- ubuntu配置jdk环境
- Hadoop:解析Partition
- Tomcat安装及配置
- href="#"与href="javascript:void(0)"的区别
- caffe的Leveldb格式数据二(详解)
- 记一次驴唇不对马嘴的DIY之旅(三)
- debian ubuntu dpkg 命令
- latex 中文标签乱码(winedt 8.1)
- 关于 @synchronized,这儿比你想知道的还要多
- 求eclipse java heap space 问题解决方案
- C# 所谓的跨类跨线程访问控件
- JPA总结
- Linux之Redhat系列程序包管理
- 一个apk文件的诞生
- 深入理解Hadoop Partitioner