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 ...
随机推荐
- 1、android源代码下载与跟踪
学习Android源代码的目的 理解Android API查找API(Activity.Content Provider等) 高级应用开发(ROM定制) 在不同平台下载Android源代码 W ...
- Golang 做的几个跟性能有关的工具
1.Vegeta https://github.com/tsenart/vegeta Vegeta 是一个万能的 HTTP 负载测试工具,提供命令行工具和一个开发包. 使用方法: $ vegeta a ...
- python27+django创建app
python manage.py startapp polls创建一个叫polls的app 编辑文件 polls/models.py : 1 from django.db import models ...
- 【LeetCode】27 - Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- Spark给我们带来了什么惊喜?
Spark的一站式解决方案有很多的优势,具体如下.(1)打造全栈多计算范式的高效数据流水线 Spark支持复杂查询. 在简单的“map”及“reduce”操作之外,Spark还支持SQL查询. ...
- Controlling GameObjects Using Components
[Accessing Components] The most common case is where a script needs access to other Components attac ...
- labview中的文件格式
- G450 CPU 升级
T系列是正常功耗的CPU,功耗35W,发热量大些, P系列是低功耗的U,功耗25W,发热量小些. P8700的性能比T6600高15%左右,不过平常应用感觉不是很明显. p8800cpu P8600 ...
- Python流程控制语句(Control Flow)
Python用于流程控制的语句包括if,for,while,这些都是从C语言借鉴过来的,然后我们会提到pass语句. 1,if if的语法很简答,我们举一个例子就好,注意关键字if, elif, el ...
- svg base64
好多h5页面有出现data:image/png;base64,后面跟了一串类似乱码的字母 查了下原来这也是svg或者是图片 <img src=“data:image/png;base64,iVB ...