C. 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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
const int INF = 0x7fffffff;
const double EXP = 1e-;
const int MS = ;
const int cnt = ;
int n;
char str[MS][MS];
bool link[cnt][cnt];
int in[cnt]; //入度
char ans[cnt];
bool have;
void input()
{
cin >> n;
for (int i = ; i < n; i++)
cin >> str[i]; memset(in, , sizeof(in));
memset(link, false, sizeof(false));
have = true;
for (int i = ; i < n - &&have; i++)
{
int len1 = strlen(str[i]);
int len2 = strlen(str[i + ]);
int ok = true;
for (int j = ; j < len1&&j < len2&&ok; j++)
{
if (str[i][j] != str[i + ][j])
{
ok = false;
if (!link[str[i][j] - 'a'][str[i + ][j] - 'a'])
{
link[str[i][j] - 'a'][str[i + ][j] - 'a'] = true;
in[str[i + ][j] - 'a']++;
}
}
}
if (ok&&len1 > len2)
{
have = false;
}
}
} void solve()
{
if (!have)
{
cout << "Impossible" << endl;
return;
}
queue<int > que;
int num = ;
for (int i = ; i < cnt; i++)
if (in[i] == )
{
que.push(i);
ans[num++] = 'a' + i;
}
while (!que.empty())
{
int s = que.front();
que.pop();
for (int i = ; i < cnt; i++)
{
if (link[s][i])
{
in[i]--;
if (in[i] == )
{
ans[num++] = 'a' + i;
que.push(i);
}
}
}
}
if (num < cnt)
cout << "Impossible" << endl;
else
{
ans[num] = '\0';
cout << ans << endl;
}
} int main()
{
input();
solve();
return ;
}
C. Fox And Names的更多相关文章
- 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 ...
- CF Fox And Names (拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- codeforce 510C Fox And Names(拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- (CodeForces 510C) Fox And Names 拓扑排序
题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...
- Fox And Names
Description Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce ...
- [CF #290-C] Fox And Names (拓扑排序)
题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...
- 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names
题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...
- codeforces 510 C Fox And Names【拓扑排序】
题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入 ...
- CodeForces 510C Fox And Names (拓扑排序)
<题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...
随机推荐
- Chapter4:表达式
左值和右值 当一个对象被用作右值的时候,用的是对象的值(内容),当对象被用作左值的时候,用的是对象的身份(在内存中的位置). 一个重要的原则是需要右值的地方可以用左值来代替,但是不能把右值当作左值使用 ...
- bzoj 2095: [Poi2010]Bridges(二分法+混合图的欧拉回路)
[题意] 给定n点m边的无向图,对于边u,v,从u到v边权为c,从v到u的边权为d,问能够经过每条边一次且仅一次,且最大权值最小的欧拉回路. [思路] 二分答案mid,然后切断权值大于mid的边,原图 ...
- TJOI2013 DAY2
第一题:明显先处理出最终序列,然后用线段树求解.处理最终序列可以用二分加树状数组(时间复杂度log2n, 用平衡树也可以搞...). /* * Problem: TJOI2013-day2-Seque ...
- js滑动门及对像的使用
function scrollDoor() { } scrollDoor.prototype = { sd: function (menus, divs, openClass, closeClass) ...
- 5个让人激动的Java项目
每个Java/JVM领域的技术专家都应从那些项目中获益,他们中有2011在SanJose举办的Jax 会议中提名为最好的技术产品奖的.我之所以选择它们,是因为它们可以广泛用于一系列的项目中,解决真实问 ...
- Apache Spark Tachyon的简介
Tachyon是一个分布式内存文件系统,可以理解为内存中的HDFS. 为了提供更高的性能,将数据存储剥离Java Heap. 用户可以基于Tachyon实现RDD或者文件的跨应用共享,并提供高容错机制 ...
- CSS选择器的特殊性
在我们为元素添加样式的时候,或多或少会出现一个元素会有几个不同规则的样式.有#id的,有.class,直接标签元素的,还有各种组合起来的选择器.那CSS到底如何解决这些冲突呢,我们这次专门来探讨一下. ...
- CodeForces 689B Mike and Shortcuts (bfs or 最短路)
Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...
- 递归函数recursion
1(调用自身)递归函数是‘自己调用自己‘的函数,不管这样的调用是直接的还是间接的. 2(递归出口)因为函数不可以一直调用自己,无法停止工作,所以递归函数一定具备结束条件. http://www.cnb ...
- js获取服务端IP及端口及协议
alert("location:"+window.location); alert("href: "+window.location.href); alert( ...