codeforces 510 C Fox And Names【拓扑排序】
题意:给出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【拓扑排序】的更多相关文章
- (CodeForces 510C) Fox And Names 拓扑排序
题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...
- CodeForces 510C Fox And Names (拓扑排序)
<题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...
- [CF #290-C] Fox And Names (拓扑排序)
题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...
- CF Fox And Names (拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF510C Fox And Names——拓扑排序练习
省委代码: #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> # ...
- 【codeforces 510C】Fox And Names
[题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...
- 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 ...
- CodeForces 1213F (强联通分量分解+拓扑排序)
传送门 •题意 给你两个数组 p,q ,分别存放 1~n 的某个全排列: 让你根据这两个数组构造一个字符串 S,要求: (1)$\forall i \in [1,n-1],S_{pi}\leq S _ ...
- Codeforces 510 A.Fox and Snake
题目链接:http://codeforces.com/contest/510/problem/A A. Fox And Snake time limit per test2 seconds memor ...
随机推荐
- POJ 3181 Dollar Dayz (完全背包,大数据运算)
题意:给出两个数,n,m,问1~m中的数组成n,有多少种方法? 这题其实就相当于 UVA 674 Coin Change,求解一样 只不过数据很大,需要用到高精度运算... 后来还看了网上别人的解法, ...
- HDU 2602 Bone Collector (简单01背包)
Bone Collector http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , i ...
- 深入浅出ES6(十六):模块 Modules
作者 Jason Orendorff github主页 https://github.com/jorendorff 早在2007年我刚加入Mozilla的JavaScript团队的时候广为流传一个 ...
- mysql limit
select * from tablename limit 1,4即取出第2条至第5条,4条记录
- FastJson与Gson小测试
最近用到Json来传输数据,找到两个比较简单的工具 Gson 和 FastJson随便测试一下两个工具的效率~ 1 package com.json.fast; import java.util.Ar ...
- C#反射技术的相关使用方法
1.获取同一程序集的类型实例 无参数构造函数 Type t=Type.GetType("AppCode.Employee"); object emp=t.Assembly.Crea ...
- TWinControl与TControl的覆盖函数(TWinControl对TControl的10个消息覆盖函数,17个覆盖函数,私有虚函数仍可多态)
手工找出来,对比一下,有助于VCL框架的理解.----------------------------------------------------------------------------- ...
- [iOS]SourceTree+oschina实现代码远程托管
在iOS开发, 涉及到多人协同开发的时候, 这个时候, 我们就得利用版本控制系统(例如GIT), 来合并和管理代码了, 今天我们来讲一下, 利用 SourceTree+oschina进行版本控制 先来 ...
- oracle11g 修改字符集 修改为ZHS16GBK
1.cmd下,cd到oracle数据库软件的服务器端 如:D:\app\Administrator\product\11.2.0\dbhome_1\BIN 2.输入set ORACLE_SID=你想进 ...
- Linux(CentOS)文件操作命令
touch命令 — 功能说明:生成新的空文件或更改现有文件的时间戳. — 命令格式:touch [参数] <文件> … — 常用参数: -a : 只更改访问时间. -m : 只更改修改时间 ...