CF Fox And Names (拓扑排序)
2 seconds
256 megabytes
standard input
standard output
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 inlexicographical 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 siand ti according to their order in alphabet.
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.
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).
3
rivest
shamir
adleman
bcdefghijklmnopqrsatuvwxyz
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Impossible
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
aghjlnopefikdmbcqrstuvwxyz
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
acbdefhijklmnogpqrstuvwxyz
拓扑排序第一题,注意特判一下下面的串是上面的串的前缀这种情况,直接输出Impossible了。
#include <bits/stdc++.h>
using namespace std; bool G[][];
int IN[];
int ANS[]; void toposort(void);
int main(void)
{
int n;
char name[][];
bool flag; cin >> n;
for(int i = ;i < n;i ++)
cin >> name[i];
for(int i = ;i < n - ;i ++)
{
for(int j = ;name[i][j] && name[i + ][j];j ++)
{
flag = false;
if(name[i][j] != name[i + ][j])
{
if(!G[name[i][j] - 'a'][name[i + ][j] - 'a'])
{
G[name[i][j] - 'a'][name[i + ][j] - 'a'] = true;
IN[name[i + ][j] - 'a'] ++;
}
flag = true;
break;
}
}
if(!flag && strlen(name[i]) > strlen(name[i + ]))
{
puts("Impossible");
return ;
}
}
toposort(); return ;
} void toposort(void)
{
queue<int> que;
int sum = ;
int k = ;
int front; for(int i = ;i < ;i ++)
if(!IN[i])
{
ANS[k ++] = i;
sum ++;
que.push(i);
} while(!que.empty())
{
front = que.front();
que.pop();
for(int i = ;i < ;i ++)
if(G[front][i])
{
IN[i] --;
if(!IN[i])
{
ANS[k ++] = i;
que.push(i);
sum ++;
}
}
}
if(sum < )
puts("Impossible");
else
{
for(int i = ;i < ;i ++)
printf("%c",ANS[i] + 'a');
puts("");
} return ;
}
CF Fox And Names (拓扑排序)的更多相关文章
- [CF #290-C] Fox And Names (拓扑排序)
题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...
- CodeForces 510C Fox And Names (拓扑排序)
<题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...
- (CodeForces 510C) Fox And Names 拓扑排序
题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...
- CF510C Fox And Names——拓扑排序练习
省委代码: #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> # ...
- CF 274D Lovely Matrix 拓扑排序,缩点 难度:2
http://codeforces.com/problemset/problem/274/D 这道题解题思路: 对每一行统计,以小值列作为弧尾,大值列作为弧头,(-1除外,不连弧),对得到的图做拓扑排 ...
- CF 213A Game(拓扑排序)
传送门 Description Furik and Rubik love playing computer games. Furik has recently found a new game tha ...
- codeforce 510C Fox And Names(拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names
题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...
- CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序
题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...
随机推荐
- Tomcat线程池,更符合大家想象的可扩展线程池
因由 说起线程池,大家可能受连接池的印象影响,天然的认为,它应该是一开始有core条线程,忙不过来了就扩展到max条线程,闲的时候又回落到core条线程,如果还有更高的高峰,就放进一个缓冲队列里缓冲一 ...
- Spark生态之Spark Core
- Struts2通配符映射
1.一个Web 应用可能有成百上千个 action 声明. 可以利用 struts 提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系 2.通配符映射规则 –若找到多个匹配, 没有通配符的 ...
- centos5 vim升级到7.4
vim在centos中的版本为7.0,导致很多插件都无法使用,所以想到升级一下. wget ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2 tar jvz ...
- Linux命令(1)-scp
Linux scp命令用于Linux之间复制文件和目录,包括从本地复制到远程.从远程复制到本地是两种使用方式. 命令基本格式 scp [可选参数] file_source file_target 从本 ...
- ActiveX控件是什么?
一.ActiveX的由来 ActiveX最初只不过是一个商标名称而已,它所涵盖的技术并不是各自孤立的,其中多数都与Internet和Web有一定的关联.更重要的是,ActiveX的整体技术是由Micr ...
- IoC/DI
From:http://jinnianshilongnian.iteye.com/blog/1471944 我对IoC/DI的理解 博客分类: spring杂谈 IoCDI IoC IoC: Inv ...
- List、ArrayList、Vector及map、HashTable、HashMap分别的区别
一.List与ArrayList的区别 List->AbstractList->ArrayList (1) List是一个接口,ArrayList是一个实现了List接口 ...
- JavaWeb学习篇之----容器Response详解
今天在来看一下Response容器的相关知识,其实这篇blog早就应该编写了,只是最近有点忙,所以被中断了.下面我们就来看一下Response容器的相关知识吧.Response和我们即将在后面说到的R ...
- VLAN设置
A Logical Network is a way of representing networks in your datacenter that have the same connectivi ...