题目链接: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 maven安装配置

    1.Run the wget command from the dir you want to extract maven too. wget http://mirrors.cnnic.cn/apac ...

  2. IDH2.5.1. Pain Points

    1. On Redhat 6.2 after uninstalling a cluster, and re-install IDH 2.5.1, you meet a "can not wr ...

  3. JVM-并发-线程安全与锁优化

    线程安全与锁优化 1.线程安全 (1)当多个线程访问一个对象时,如果不考虑这些线程在执行时环境下的调度和交替执行,也不需要进行额外的同步,或者在调用方进行任何其他的协调操作,调用这个对象的行为都可以获 ...

  4. Android Studio安装更新终极解决方式

    之前写过一篇Android SDK无法更新的博文,其实该方式对Android Studio同样有效,大伙可以下载网盘中分享的小软件,若搜索到通道后提示需要更细,也可以选择更新.参考:http://bl ...

  5. java.lang.ClassNotFoundException: com.*.listener.ConfigInfoReader

    包的问题,删除掉了buildPath的包,再加上一个需要用的,问题搞定...不知道是为什么

  6. HDU 5669 线段树优化建图+分层图最短路

    用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...

  7. Python KNN算法

    机器学习新手,接触的是<机器学习实战>这本书,感觉书中描述简单易懂,但对于python语言不熟悉的我,也有很大的空间.今天学习的是k-近邻算法. 1. 简述机器学习 在日常生活中,人们很难 ...

  8. Jena TDB 102

    1 Introduction TDB is a RDF storage of Jena. official guarantees and limitations TDB support full ra ...

  9. [Debian]8.2升8.3

    $ uname -mrs $ lsb_release -a $ sudo apt-get update#開始升級 $ sudo apt-get dist-upgrade $ sudo reboot#重 ...

  10. JAVA生成TXT日志文件

    /** * 生成日志文件(文件的位置在Tomcat的安装路径下) * @param str */ public static void LogForTXT(String str) { try { St ...