[CF #290-C] Fox And Names (拓扑排序)
题目链接:http://codeforces.com/contest/510/problem/C
题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来。
递归建图:竖着切下来,将每个名字的第x个字母从上到下连接建图。然后求拓扑排序。
之所以要拓扑排序,因为要判断在x-1里面有a-->b 在x中有b-->a,这样就形成了一个环。这样一来,就不能够构造字母表了。
【经验教训】:在递归建图的函数中开了两个数组,用来记录字母第一次出现和最后一次出现的位置。。结果就RE在12上。。今后尽量不要在递归函数中开数组。。
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <set>
#include <vector>
#include <iterator>
#include <cstring>
#include <map>
#include <cctype>
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std; int n;
string names[];
bool hasAns = true;
vector<int> G[];
int c[],topo[],t; void solve(int l,int r,int x){
if( l>=r ) return;
if( !hasAns ) return;
for(int i=l;i<=r;i++){
if( x>=names[i].size() ) {
hasAns = false;
return;
}
int j = i+;
for(;j<=r;j++){
if( names[i][x]==names[j][x] ){
continue;
} else {
break;
}
}
while( names[i].size()-<x+&&i<j- ) i++;
solve(i,j-,x+);
i = j-;
} for(int i=l;i<r;i++) {
if( names[i][x]==names[i+][x] ) continue;
G[names[i][x]-'a'].push_back(names[i+][x]-'a');
}
} bool dfs(int u){
c[u] = -;
for(int i=;i<G[u].size();i++){
int v = G[u][i];
if( v< ) continue;
if( c[v]< ) return false;
else if(!c[v] && !dfs(v) ) return false;
}
c[u] = ; topo[--t] = u;
return true;
} bool toposort(){
t = ;
memset(c,,sizeof(c));
for(int u=;u>=;u--) if(!c[u])
if(!dfs(u)) return false;
return true;
} int main(){
cin >> n;
for(int i=;i<n;i++) cin >> names[i];
solve(,n-,);
if( !hasAns ) {
puts("Impossible");
return ;
}
bool hasAns = toposort();
if( !hasAns ) {
puts("Impossible");
return ;
}
for(int i=;i<;i++){
putchar('a'+topo[i]);
}
puts(""); return ;
}
[CF #290-C] 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 ...
- CodeForces 510C 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 ...
- 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 ...
- codeforce 510C Fox And Names(拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #290 (Div. 2) 拓扑排序
C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...
- CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序
题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...
随机推荐
- Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法:
Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用: Activity的 onSaveInstan ...
- Ubuntu下快速安装LAMP server
Ubuntu下可快速安装LAMP server(Apache+MySQL+PHP5). 首先,打开Ubuntu虚拟机,Terminal打开root权限:“sudo -s”. 一.安装LAMP serv ...
- 用CSS定义每段首行缩进2个字符 转
应该遵循w3c所制定的html/xhtml标准来使用tag和编写网页.如果你对此不太了解,可以到w3c的网站www.w3.org去找相关资料,或者买一本xhtml的书(注意不要买过时的html的书,尽 ...
- vs2012 vs2010 opencv2.4.7配置
http://blog.csdn.net/xiaohanstu/article/details/45309149?utm_source=tuicool <pre name=.7配置 .配置 () ...
- 百度之星热身赛-1001(dfs拓扑排序)
题意:作为年度优秀魔法学员的奖赏,哈利得到了一台具有魔力的计算机.这台计算机一旦开始处理某个任务,就会一直处理到这个任务结束为止(所以你可以认为它是单线程的).有一天,这台计算机得到了n个任务要处理, ...
- BNUOJ 13105 nim博弈
ncredible Chess Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on LightOJ. Ori ...
- HDU 4471 矩阵快速幂 Homework
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...
- html5本地存储的解决
1.解决了Cookie 4K存储大小的问题2.解决了请求头常带存储信息的问题3.解决了关系型存储的问题4.跨域问题,跨浏览器*在 HTML5 中,数据不是由每个服务器请求传递的,而是只有在请求时使用 ...
- CSS值得关注的那些事?
CSS值得关注的那些事? CSS层叠样式表的出现,使得样式表现层与HTML结构层分离,CSS样式与HTML结构层次清晰,一目了然.给我们前端开发者带来诸多的便利,然而,在实际前端开发中,还是有相当多的 ...
- isinstance(),issubclass()
isinstance(object,classinfo) 返回True,如果object是classinfo或者classinfo子class的实例. 如果classinfo是包含type和class ...