codeforce 510C 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 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.
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 大坑点:
2
aa
a
这应该是impossible 题意:有n个按照某一字母表排好序的字符串,问是否存在这样的字母表。或者说是否存在这样一张字母表使字符串排序后是这个样子。
思路:可以根据字符串的顺序得出一些图的指向,比如abc和abd,可以得到c->d,这样就可以得到若干边,根据这个进行拓扑排序即可。注意有个陷阱是如果存在xy>x,那就无解。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
char a[][];
int in[];
int out[];
bool book[];
vector<int>v[];
queue<int>q;
int main()
{
int n;scanf("%d",&n);
memset(book,,sizeof(book));
memset(in,,sizeof());
memset(out,,sizeof());
while(!q.empty()) q.pop();
for(int i=;i<=n;i++)scanf("%s",&a[i]);
for(int i=;i<=n;i++)
{ for(int j=i+;j<=n;j++)
{
int l=min(strlen(a[i]),strlen(a[j]));
for(int k=;k<l;k++)
{
if(a[i][k]!=a[j][k])
{
in[a[j][k]-'a'+]++;//保存入度
out[a[i][k]-'a'+]++;//其实不用考虑出度的
v[a[i][k]-'a'+].push_back(a[j][k]-'a'+);
break;
}
if(k==l-&&strlen(a[i])>strlen(a[j]))
{
printf("Impossible");
return ;
}
}
}
}
//cout<<in['e'-'a'+1]<<endl;
// cout<<in['p'-'a'+1]<<endl;
int k=;
while()
{
int x=-;
for(int i=;i<=;i++)
{
if(in[i]==&&!book[i])//找到没有入度的点
{
x=i;
break;
}
}
if(x==-&&k<)
{
//cout<<k<<endl;
cout<<"Impossible";
break;
}
if(x==-) break;
k++;
book[x]=;
q.push(x);
for(int i=;i<v[x].size();i++)//将挑选出来的改点的边都去掉
{
in[v[x][i]]--;
}
}
if(k==)
{
while(!q.empty())
{
char t=q.front()+'a'-;
cout<<t;
q.pop();
}
}
return ;
}
codeforce 510C Fox And Names(拓扑排序)的更多相关文章
- CodeForces 510C Fox And Names (拓扑排序)
<题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...
- CF Fox And Names (拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- [CF #290-C] Fox And Names (拓扑排序)
题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...
- (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> # ...
- 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names
题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...
- Codeforces 510C (拓扑排序)
原题:http://codeforces.com/problemset/problem/510/C C. Fox And Names time limit per test:2 seconds mem ...
- 【codeforces 510C】Fox And Names
[题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...
- C. Fox And Names
C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- 39条常见的linux系统管理面试题
1.如何看当前Linux系统有几颗物理CPU和每颗CPU的核数? 答:[root@centos6 ~ 10:55 #35]# cat /proc/cpuinfo|grep -c 'physical i ...
- 多校hdu5738 寻找
这道题前面给了两个公式,其实仔细分析一下,就会发现其实是给了你一堆点的坐标,然后让你求这些点有多少种组合可以形成共线的情况当两个点在一个坐标上时这两个点可以看做是不同的两个点,也就是说如果两个点在一个 ...
- java深入探究12-框架之Structs
注意每次修改配置文件后必须项目重启 Structs2=structs1+xwork Struct2框架预先实现了一些功能: 1.请求数据的封装:2.文件上传的功能3.对国际化功能的简化4.文件效验功能 ...
- BZOJ4199/UOJ131 [Noi2015]品酒大会
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 搜索6--noi1700:八皇后问题
搜索6--noi1700:八皇后问题 一.心得 二.题目 1756:八皇后 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 会下国际象棋的人都很清楚:皇后可以 ...
- java: scanner(输入流)文本扫描类
//scanner是接受system.in输入流的操作类 //scanner同时也支持文件输入流的操作 //一个可以使用正则表达式来分析基本类型和字符串的简单文本扫描器 Scanner scan = ...
- Nodejs+MongoDB+Bootstrap+esj搭建的个人简易博客
github:https://github.com/yehuimmd/myNodeBloy Nodejs+MongoDB+jQuery+Bootstrap-esj搭建的个人简易博客 主要功能 前台 : ...
- android横屏布局文件设置
一.AndroidManifest.xml配置 1.在AndroidManifest.xml的activity(需要禁止转向的activity)配置中加入 android:screenOrient ...
- /var/adm/wtmp文件太大该怎么办?
/var/admin/wtmp文件记录所有用户的登陆信息,随着时间会增长到很大,/var/adm/wtmp文件太大该怎么办呢?先来看看/var/adm/wtmp文件的属性:testterm1:/#ls ...
- JS循环遍历JSON数据的方法
JSON数据如:{"options":"[{/"text/":/"王家湾/",/"value/":/" ...