题目链接: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 (拓扑排序)的更多相关文章

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

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

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

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

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

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

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

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

  5. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

  6. codeforce 510C Fox And Names(拓扑排序)

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

  7. Codeforces Round #290 (Div. 2) 拓扑排序

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

  8. 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 ...

  9. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

随机推荐

  1. 如何在Linux下使用Gitblit工具创建Git仓库服务

    嗨!朋友,今天我们将学习如何在你的Linux服务器或者PC上安装Gitblit工具.首先,我们看看什么是Git,它的功能以及安装Gitblit的步骤.Git是分布式版本控制系统,它强调速度.数据一致性 ...

  2. CodeForces 686B-Little Robber Girl's Zoo

    题目: 有n头数量的动物,开始它们站在一排,它们之间有高度差,所以需要将它们进行交换使得最终形成一个不减的序列,求它们交换的区间.交换的规则:一段区间[l, r]将l与l+1.l+2与l+3..... ...

  3. 实现手机扫描二维码页面登录,类似web微信-第三篇,手机客户端

    转自:http://www.cnblogs.com/fengyun99/p/3541254.html 上一篇,介绍了二维码生成的机制,紧接着,我们就要开发手机客户端来识别这个二维码. 二维码,实际上是 ...

  4. iOS view 颜色渐变

    //渐变色过渡自然 CAGradientLayer *layer = [CAGradientLayer layer]; layer.frame = CGRectMake(0, 0, curW-10,4 ...

  5. BZOJ 1833 count 数字计数

    sb数位dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...

  6. Bellovin_树状数组

    Problem Description Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F( ...

  7. CI框架源码分析

    这几天,把ci源码又看了一遍,于是有了新的收获.明白了在application目录下core文件夹的作用,就是用来写ci核心文件的扩展的, 而且需要在配置文件中添加类前缀MY_. CI框架整体是但入口 ...

  8. Phaser开源2d引擎 html5游戏框架中文简介

    功能特点(Features) 易维护代码(Easy Asset Loading) Phaser可以加载图片,音频文件,数据文件,文本文件和自动解析精灵图和纹理地图集数据(出口纹理封隔器或Flash C ...

  9. ListView去除顶部和底部边缘阴影(亲测4.4及以前的版本都适用)

    ListView滑动到顶部和底部时出现的阴影消除方法:android2.3以前用android:fadingEdge="none"android2.3以后用android:over ...

  10. js中有趣的闭包(closure)

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...