[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 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...
随机推荐
- web前端基础篇⑧
1.伪类选择器 都以冒号开始.:focus 焦点的地方加样式:first-child 向元素的第一个子元素添加样式锚伪类:a:link {color:red} 未访问的链接 a:visited {co ...
- 模拟jquery实现each方法和map方法
********************each方法********************** function each( obj, cbk ) { /* * 实现思路: * 1.首先却分传入进来 ...
- Objective-C学习笔记-第二天(1)
Objective-C中,调用方法采用的是一种消息传递机制. 参考文章:http://blog.csdn.net/xingyevc/article/details/39397873 如果向某个对象传递 ...
- python模块之time
Python中的时间模块. 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素. 2.UTC(Coordinated U ...
- Collection(数组、字典、集合)
Collection -NSArray和NSMutableArray +array:创建一个空数组 +arrayWithArray:从另一个数组创建新的数组 ...
- SpringMVC 用http请求的Get和Post请求作为路由的方法的重载方式
@Controller @RequestMapping("/messageProcessing") public class WechatPushController { @Aut ...
- Spring 和SpringMVC 的父子容器关系
Spring和SpringMVC作为Bean管理容器和MVC层的默认框架,已被众多WEB应用采用,而实际使用时,由于有了强大的注解功能,很多基于XML的配置方式已经被替代,但是在实际项目中,同时配 ...
- [Codeforces Round #275 (Div. 2)]B - Friends and Presents
最近一直在做 codeforces ,总觉得已经刷不动 BZOJ 了? ——真是弱喵 你看连 Div.2 的 B 题都要谢谢题解,不是闲就是傻 显然我没那么闲 ╮(╯_╰)╭ 我觉得这题的想法挺妙的~ ...
- My Code Snippet
加载菜单(js) function mkmenu(menus,name,id,parentid,url) { $.each(menus,function(i){ menus[i].children=n ...
- timer--计时器
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...