题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序

先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度

然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入队列中,并将它指向的节点的入度减去1

另外如果len[i]<len[i-1],说明第i个字符串是i-1个字符串的前缀,但是它还排在后面,这样肯定不存在符合的字典序,直接输出“Impossible”

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int e[maxn],first[maxn],next[maxn],ans[maxn],len[maxn],vis[maxn],in[maxn];
char s[][];
int ecnt; void addedges(int u,int v){
e[ecnt]=v;
next[ecnt]=first[u];
first[u]=ecnt;
in[v]++;
ecnt++;
} int main(){ // freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n;
cin>>n; memset(first,-,sizeof(first));
for(int i=;i<=n;i++){
cin>>s[i];
len[i]=strlen(s[i]);
} int j;
ecnt=;
for(int i=;i<=n;i++){
for(j=;j<min(len[i],len[i-]);j++){
if(s[i-][j]!=s[i][j]){
addedges(s[i-][j]-'a',s[i][j]-'a');
break;
}
}
if(len[i]<len[i-]&&j==len[i]){
printf("Impossible\n");
return ;
}
} memset(vis,,sizeof(vis)); for(int i=;i<=;i++){
int jj=;
int j;
for( j=;j<;j++){
if(!vis[j]&&in[j]==){
vis[j]=;
ans[i]=j;
for(int k=first[j];k!=-;k=next[k]){
in[e[k]]--;
}
jj=j;
break;
}
}
if(jj==){//jj的值没有发生改变,说明这 一点找不到符合的拓扑序
printf("Impossible\n");
return ;
}
} for(int i=;i<=;i++)
printf("%c",ans[i]+'a');
printf("\n");
return ;
}

codeforces 510 C Fox And Names【拓扑排序】的更多相关文章

  1. (CodeForces 510C) Fox And Names 拓扑排序

    题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...

  2. CodeForces 510C Fox And Names (拓扑排序)

    <题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...

  3. [CF #290-C] Fox And Names (拓扑排序)

    题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...

  4. CF Fox And Names (拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. CF510C Fox And Names——拓扑排序练习

    省委代码: #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> # ...

  6. 【codeforces 510C】Fox And Names

    [题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...

  7. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

    C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

  8. CodeForces 1213F (强联通分量分解+拓扑排序)

    传送门 •题意 给你两个数组 p,q ,分别存放 1~n 的某个全排列: 让你根据这两个数组构造一个字符串 S,要求: (1)$\forall i \in [1,n-1],S_{pi}\leq S _ ...

  9. Codeforces 510 A.Fox and Snake

    题目链接:http://codeforces.com/contest/510/problem/A A. Fox And Snake time limit per test2 seconds memor ...

随机推荐

  1. eclipse luna使用jdk1.8初始化

    1.安装jdk版本1.8 2.配置环境变量 D:\Program Files\Java\jdk1.8.0_11 (如果从jdk7更改为jdk8,修改JAVA_HOME变量就可以了.)JAVA_HOME ...

  2. HDU 1260 Tickets(简单dp)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  3. 两台笔记本搭建openvswitch网络

    环境说明: 笔记本A.B均运行Ubuntu 14.04,两台笔记本通过无线网卡上网,用一根网线连接两台笔记本的有线网卡. 网络拓扑: 其中,vm1 vm2 S1位于笔记本A,vm3 vm4 S2位于笔 ...

  4. C# Socket 入门4 UPD 发送结构体(转)

    今天我们来学 socket  发送结构体 1. 先看要发送的结构体 using System; using System.Collections.Generic; using System.Text; ...

  5. jquery plug-in DataTable API中文文档参考

    前言:最近在做一个WEB后台,无意中发现这个插件,试用了一下觉得不错,但网上关于它的资料大多不全,所以利用一些时间将其API文档翻了一下,发在园子里供大家参考.(p.s:个人E文水平很差,对着灵格斯翻 ...

  6. qt之esc键

    Esc键对大家来说实在熟悉不过的了,在Qt中Esc键也会默认的进行一些事件的触发,今天对Esc键测试了一下,突然发现不像我想象的那样,在QDialog中按下Esc键会默认调用reject()方法而不是 ...

  7. 打印TMemo的内容到打印机

    Canvas.TextOut真是好用,Printer也实在好用: procedure PrintTStrings(Lst : TStrings) ; var I, Line : Integer; be ...

  8. JavaWeb笔记——利用过滤器实现页面静态化

    1.说明 页面静态化是把动态页面生成的html保存到服务器的文件上,然后再有相同请求时,不再去执行动态页面,而是直接给用户响应上次已经生成的静态页面. * 核心思路为拦截请求,实现请求转发指向静态页面 ...

  9. 282. Expression Add Operators

    题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...

  10. MyEclipse6.6 汉化过程

                                                                                                         ...