Codeforces题号:#510C

出处: Codeforces

主要算法:判环+拓扑

难度:4.2

思路分析:

要是把这道题联系到图上就很容易想了。

  如何建图?由于最后要求名字满足字典序,所以不妨以字母为节点,然后按照题意的顺序从小的到大的连边。建图了又什么用?如果图存在环,那么也就意味着矛盾了——因为这比自己小的节点比自己大。因此是否存在一个合法的字典序的判断依据就是建的图是否存在环。

  第二步,如何输出任意一个方案?这很简单,由于有可能有的字母根本没有参与建图,所以这些字母就不需要管了。对于在图里的字母,入度为0的点就是已知的应当最小的点——所以我们进行一次拓扑排序来得到序列。最后和其它字母拼合起来就好了。

代码注意点:

  注意有两个相同前缀的字符串时,如果前面的那个字符串比后面的还要长,那么一定不满足,直接输出impossible就好了。因为无论字典序如何,都不可能满足前面的字典序比后面的小。

Code

/** This Program is written by QiXingZhi **/
#include <cstdio>
#include <queue>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int N = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar();
return x * w;
}
string s[N];
queue <int> q;
vector <int> G[];
bool exist[N],vis[N],b[N],hh[N];
int rd[N],Q[N],ans_topo[N];
int n,len1,len2,flg,_cur,t,topo_num;
inline void AddEdge(int u, int v){
exist[u] = ;
exist[v] = ;
G[u].push_back(v);
++rd[v];
}
inline void ThrowOut(){
printf("Impossible");
exit();
}
inline bool BFS(int x){
while(!q.empty()) q.pop();
q.push(x);
int cur,sz,to;
while(!q.empty()){
cur = q.front();
q.pop();
if(cur == _cur){
if(flg == -){
++flg;
}
else if(flg == ){
return ;
}
}
if(vis[cur]) continue;
vis[cur] = ;
sz = G[cur].size();
for(int i = ; i < sz; ++i){
to = G[cur][i];
q.push(to);
}
}
return ;
}
inline bool Check_Circle(){
for(int i = 'a'; i <= 'z'; ++i){
memset(vis,,sizeof(vis));
if(exist[i]){
_cur = i;
flg = -;
if(BFS(_cur)) return ;
}
}
return ;
}
int main(){
// freopen(".in","r",stdin);
cin >> n;
for(int i = ; i <= n; ++i){
cin >> s[i];
}
for(int i = ; i <= n; ++i){
len1 = s[i-].size();
len2 = s[i].size();
flg = ;
for(int j = ; j < len2; ++j){
if(j >= len1){
flg = ;
break;
}
if(s[i-][j] != s[i][j]){
flg = ;
AddEdge(s[i-][j],s[i][j]);
break;
}
}
if(flg == && len1 > len2){
ThrowOut();
}
}
if(Check_Circle() == ){
ThrowOut();
}
int flg=,sz;
for(;;){
flg = , t = ;
for(int i = 'a'; i <= 'z'; ++i){
if(rd[i] == && !b[i] && exist[i]){
flg = ;
b[i] = ;
Q[++t] = i;
}
}
if(!flg) break;
for(int i = ; i <= t; ++i){
int cur = Q[i];
ans_topo[++topo_num] = cur;
hh[cur] = ;
sz = G[cur].size();
for(int j = ; j < sz; ++j){
--rd[G[cur][j]];
}
}
}
for(int i = ; i <= topo_num; ++i){
printf("%c",ans_topo[i]);
}
for(int i = 'a'; i <= 'z'; ++i){
if(!hh[i]){
printf("%c",i);
}
}
return ;
}

Codeforces510 C. Fox And Names的更多相关文章

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

  2. C. Fox And Names

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

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

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

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

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

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

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

  6. Fox And Names

    Description Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce ...

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

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

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

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

  9. codeforces 510 C Fox And Names【拓扑排序】

    题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入 ...

随机推荐

  1. 【全网最全的博客美化系列教程】01.添加Github项目链接

    全网最全的博客美化系列教程相关文章目录 [全网最全的博客美化系列教程]01.添加Github项目链接 [全网最全的博客美化系列教程]02.添加QQ交谈链接 [全网最全的博客美化系列教程]03.给博客添 ...

  2. filebeat 源码编译安装

    下载filebeat源码(6.2.3)下载地址:链接: https://pan.baidu.com/s/1cPR7-xlQJuYZ77uaUpfSpQ 提取码: k77u github下载地址:htt ...

  3. Python_装饰器进阶_32

    #带参数的装饰器 #500个函数 import time FLAGE = True def timmer_out(flag): def timmer(func): def inner(*args,** ...

  4. docker之导出、导入、数据搬迁

    docker 导出 导入有二种,一种是备份镜像,一种备份容器.数据搬迁,最简单粗暴就是直接COPY,volume的路径就行了. 一.导出导入镜像 #导出为tar docker save #ID or  ...

  5. UTC时间、GMT时间、本地时间、Unix时间戳

    引用: https://blog.csdn.net/u012102306/article/details/51538574 https://blog.csdn.net/foxir/article/de ...

  6. javaScript中闭包的工作原理

    一.什么是闭包? 官方”的解释是:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.相信很少有人能直接看懂这句话,因为他描述的太学术.其实这句话 ...

  7. synchronized无法禁止指令重排序的证明

    package demo.reorder; import java.util.concurrent.ExecutorService; import java.util.concurrent.Execu ...

  8. 简单JQuery+AJAX+Servlet的计算器实现

    index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...

  9. MySQL执行语句的顺序

    MySQL的语句一共分为11步,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入,只是这些虚拟的表对用户来说是透明的,但是只有 ...

  10. varnish4 配置文件整理

    vim default.vcl # 使用varnish版本4的格式. vcl 4.0; # 加载后端轮询模块 import directors; #######################健康检查 ...