<题目链接>

题目大意:

给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同。

解题分析:
本题想到拓扑排序就好做了。就是枚举每个字符串,每个字符串和它前一个字符串寻找第一个不同的字符,然后前一个串的该字符向当前串的字符连一条有向边(注意判断后一个串是前一个串的子串的情况)。然后就是跑一遍拓扑排序,如果不冲突的话,就可构造出答案。

#include <bits/stdc++.h>
using namespace std; #define pb push_back
string str[];
vector<int>G[];
int ind[];
vector<int>ans; inline void TopoSort(){
queue<int>q;
for(int i=;i<;i++){
if(!ind[i])q.push(i);
}
while(q.size()){
int u=q.front();q.pop();
ans.pb(u);
ind[u]=-;
for(int i=;i<G[u].size();i++){
int v=G[u][i]; //得到它的子节点
ind[v]--;
if(ind[v]==)q.push(v);
}
}
}
int main(){
int n;cin>>n;
for(int i=;i<=n;i++)
cin>>str[i];
for(int i=;i<=n;i++){ //枚举字符串
int len1=str[i-].size();
int len2=str[i].size();
bool fp=false;
for(int j=;j<min(len1,len2);j++){
if(str[i-][j]!=str[i][j]){
int u=str[i-][j]-'a';
int v=str[i][j]-'a';
G[u].pb(v);
ind[v]++; //记录这个点的入度
fp=true;
break;
}//找到第一个不同的元素
}
if(!fp&&len1>len2)return puts("Impossible"),; //如果当前串是前一个串的子串
}
TopoSort();
if(ans.size()<)puts("Impossible");
else {
for(int i=;i<ans.size();i++)
printf("%c",ans[i]+'a');
puts("");
}
}

CodeForces 510C Fox And Names (拓扑排序)的更多相关文章

  1. [CF #290-C] Fox And Names (拓扑排序)

    题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...

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

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

  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. codeforce 510C Fox And Names(拓扑排序)

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

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

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

  7. CodeForces 909E Coprocessor(无脑拓扑排序)

    You are given a program you want to execute as a set of tasks organized in a dependency graph. The d ...

  8. codeforces 915D Almost Acyclic Graph 拓扑排序

    大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现 ...

  9. Codeforces 919D:Substring(拓扑排序+DP)

    D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...

随机推荐

  1. 当前标识没有对“C:\WINDOWS\Microsoft.NET\...”的写访问权限的解决办法

    1.需要重新注册IIS服务扩展,在开始运行中输入以下命令运行:aspnet_regiis -i 32位的Windows: --------------------------------------- ...

  2. StackExchange.Redis 异步超时解决方案

    Timeout awaiting response (outbound=0KiB, inbound=45417KiB, 5891ms elapsed, timeout is 5000ms), comm ...

  3. js关于“call方法”百度,阿里超难面试题

    面试题:function fn(a,b){    console.log(this);    console.log(a);    console.log(a+b);}fn.call(1);fn.ca ...

  4. input下拉带输入框

    html5 自带的datalist实现 html代码: <input list="students"> <datalist id="students&q ...

  5. 循环队列和链式队列(C++实现)

    循环队列: 1.循环队列中判断队空的方法是判断front==rear,队满的方法是判断front=(rear+1)%maxSize.(我曾经想过为什么不用一个length表示队长,当length==m ...

  6. luogu P5304 [GXOI/GZOI2019]旅行者

    传送门 所以这个\(5s\)是SMG 暴力是枚举每一个点跑最短路,然后有一个很拿衣服幼稚的想法,就是把所有给出的关键点当出发点,都丢到队列里,求最短路的时候如果当前点\(x\)某个相邻的点\(y\)是 ...

  7. linux下进入root用户登录

    1.打开终端,输入sudo passwd -u root 输入当前用户的登录密码,提示如下标红区域信息 解决方案: 1)直接输入命令:su,输入当前用户登录密码 2)添加sudoers文件的写权限,命 ...

  8. 003 爬虫持久化的三个不同数据库的python代码

    MongoDB import pymongo # 1.连接MongoDB服务 mongo_py = pymongo.MongoClient() print(mongo_py) # 2.库和表的名字:有 ...

  9. 分布式系列九: kafka

    分布式系列九: kafka概念 官网上的介绍是kafka是apache的一种分布式流处理平台. 最初由Linkedin开发, 使用Scala编写. 具有高性能,高吞吐量的特定. 包含三个关键能力: 发 ...

  10. (Linux)初探cmake .和make命令

    cmake编译OpenCV工程 首先我们看到文件夹中有一cpp文件,CMakeLists.txt文件和一张图片 首先进行cmake .命令 接着进行make命令 . 然后就得到了可执行文件,也就是说可 ...