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 ...
随机推荐
- MVC6 (ASP.NET5) 认证 (Asp.net identity) cookie模式 自定义认证
1.Startup类的Configure方法中, app.UseIdentity(); 改为 app.UseCookieAuthentication(options => { options.A ...
- iBatis的Settings节点参数详解[转]
(1) cacheModelsEnabled: 是否启用SqlMapClient上的缓存机制.建议设为"true".默认值为“true”. (2) enhancementEnabl ...
- Nginad广告生成代码分析
大家都知道实时竞价的广告一般会在一个iframe中,这个iframe会有一个复杂的src.那么这个iframe是如何生成的? 这里分析NginAd作为exchange时,如何让媒体网站通过引用一段ad ...
- 一个简单的Javascript闭包示例
//=====用闭包实现函数的Curry化===== //数字求和函数的函数生成器 function addGenerator( num ){ //返回一个简单的匿名函数,求两个数的和,其中第一个数字 ...
- scapy学习笔记(3)
转自:@小五义:http://www.cnblogs/xiaowuyi 在安装完scapy(前两篇笔记有介绍)后,linux环境下,执行sudo scapy运行scapy. 一.简单的发送包 1.se ...
- Java -- JDBC 批处理
两种批处理方式: 采用Statement.addBatch(sql)方式实现批处理: •优点:可以向数据库发送多条不同的SQL语句. •缺点: •SQL语句没有预编译. •当向数据库发送多条语句相同, ...
- 强制关闭iPhone iPad AppleWatch MacOS
iPhone/iPad 强制关闭APP:按住Power(电源键),出现关机界面松开,按住Home键9秒左右. 强制重启/关机:同时按住Power和Home键几秒后重启,出现logo时松开Home继续按 ...
- tar 或 7z 备份项目
mac, tar #!/bin/sh projPath=~/Developer projName=youku now=`date +%Y-%m-%d-%H-%M-%S` output=$projNam ...
- MYSQL 索引创建与使用
可能用到索引的地方: where 子句,order by,group by 不需要创建索引的情况: 1. 表比较小 2.赋值有限的列(枚举),不要创建索引.创建的索引返回的行越少越好,此时区分度大. ...
- python向数据库插入中文乱码问题
1.python向数据库插入中文乱码问题 直接手动insert into 中文 不乱码,但是用程序跑起来就乱码. conn =MySQLdb.connect(host="127.0.0.1& ...