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 ...
随机推荐
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File
作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别 是:SharePreference.SQLite.Content Provider和File ...
- Linux基本命令(6)线上查询的命令
线上查询的命令 命令 功能 man 查询和解释一个命令的使用方法,以及这个命令的说明事项 locate 定位文件和目录 whatis 寻找某个命令的含义 6.1 man命令 man命令用来查询和解释一 ...
- 可进行JavaScript代码测试与调试的12个网站
概述:JavaScript是网站前端开发最为重要的一门编程语言,本文收集了能够在线测试与调试JavaScript代码的12个网站 1.JS Bin JS bin是一个为JavaScript和CSS爱好 ...
- 创建优雅表格的8个js工具
当需要呈现数百个表的数据时,展示和可访问性扮演着至关重要的角色.在这种情况下,倘若一个数据网格能够支持大量数据集的HTML Table并提供诸如排序.搜索.过滤和分页等功能,那是棒棒哒.在这篇文章中, ...
- 基类,派生类,内存分配情况 .xml
pre{ line-height:1; color:#1e1e1e; background-color:#d2d2d2; font-size:16px;}.sysFunc{color:#627cf6; ...
- eclipse 恢复被删除的文件
即使你在项目中删除了某一文件,该文件的相关信息仍会保存在本地历史记录中.这就使得你可以恢复那些在项目或文件夹中已被删除的文件.如果恢复某一被删除的文件,则首先在Navigator视图中选择该文件先前所 ...
- C++模板详解
参考:C++ 模板详解(一) 模板:对类型进行参数化的工具:通常有两种形式: 函数模板:仅参数类型不同: 类模板: 仅数据成员和成员函数类型不同. 目的:让程序员编写与类型无关的代码. 注意:模板 ...
- (转)QR二维码生成及原理
二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型:比如:字符,数字, ...
- 在适配iPhone 6 Plus屏幕的时候,模拟器上两边有很细的白边如何解决
取消掉Constrain to margin 然后添加左右约束 版权声明:本文为博主原创文章,未经博主允许不得转载.
- 成为IBM精英讲师-一分耕耘 一份收获 同时也多了一份责任!
成为IBM精英讲师 一分耕耘 一份收获 同时也多了一份责任! http://www.webspherechina.net/?action-iste-type-lecturerlist 650) thi ...