题意:给出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. Unity3D研究院之静态自动检查代码缺陷与隐患

    原地址:原地址:http://www.xuanyusong.com/archives/2828 代码缺陷和代码错误的最大区别是,代码缺陷不影响游戏编译,而代码错误编译都不通过.但是代码缺陷会影响游戏发 ...

  2. Sqli-labs less 19

    Less-19 从源代码中我们可以看到我们获取到的是HTTP_REFERER 那和less18是基本一致的,我们从referer进行修改. 还是像less18一样,我们只给出一个示例 将referer ...

  3. vi/vim使用指北 ---- Sample Editing

    本篇介绍vim的基础操作,各种编辑模式的切换,光标的移动,删除,撤销/重做,保存,查找等基础命令: 基础操作 编辑文件 vim  [options] [file ...] 模式 打开文件后进入vim的 ...

  4. HDU4784 Dinner Coming Soon(dp)

    当时区域赛的一道题.题意大概是这样的,有一个1~N的图,然后你要从1->N,其中每经过一条边需要消耗你的时间和金钱,每到一个地方可以选择什么都不做,或者买一包盐,卖一包盐,身上不能同时有超过B包 ...

  5. iOS搜索栏

    百度云连接:http://pan.baidu.com/s/1pJLzDFX

  6. GCD常用方法

    1.延迟操作 2.一次性代码 3.队列组 /** * 延迟执行 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC ...

  7. iOS-CAEmitterLayer(粒子效果)

    扩展:https://github.com/lichtschlag/Dazzle  ;     , , , ); , );     .f;     .f;     ;     .f;     .f; ...

  8. poj 2425 A Chess Game 博弈论

    思路:SG函数应用!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include< ...

  9. IP TCP HTTP Socket的区别

    网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...

  10. web.xml中servlet初始化参数的设置

    <context-param><param-name>param1</param-name><param-value>value1</param- ...