题目传送门

 /*
给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序
拓扑排序
详细解释:http://www.2cto.com/kf/201502/374966.html
*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <cmath>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
char s[][];
int in[];
bool lin[][];
char ans[]; bool TopoSort(void)
{
queue<int> q;
int cnt = ;
for (int i=; i<=; ++i)
{
if (in[i] == )
{
q.push (i);
ans[cnt++] = 'a' + i - ;
}
}
while (!q.empty ())
{
int now = q.front (); q.pop ();
for (int i=; i<=; ++i)
{
if (lin[now][i])
{
in[i]--;
if (in[i] == )
{
q.push (i);
ans[cnt++] = 'a' + i - ;
}
}
}
}
ans[] = '\0';
if (cnt < ) return false;
else return true;
} int main(void)
{
//freopen ("C.in", "r", stdin); int n; while (~scanf ("%d", &n))
{
memset (lin, false, sizeof (lin));
memset (in, , sizeof (in));
for (int i=; i<=n; ++i)
{
scanf ("%s", &s[i]);
} bool flag = true;
for (int i=; i<=n- && flag; ++i)
{
int len1 = strlen (s[i]);
int len2 = strlen (s[i+]);
bool ok = false;
for (int j=; j<=len1- && j<=len2- && !ok; ++j)
{
if (s[i][j] != s[i+][j])
{
ok = true;
if (!lin[s[i][j]-'a'+][s[i+][j]-'a'+])
{
in[s[i+][j]-'a'+]++;
lin[s[i][j]-'a'+][s[i+][j]-'a'+] = true;
}
}
}
if (!ok && len1 > len2) flag = false;
} if (!flag) puts ("Impossible");
else
{
flag = TopoSort ();
if (!flag) puts ("Impossible");
else printf ("%s\n", ans);
}
} return ;
}

拓扑排序 Codeforces Round #290 (Div. 2) 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. Codeforces Round #290 (Div. 2) D. Fox And Jumping dp

    D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...

  3. Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...

  4. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  5. Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模

    E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

  7. DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...

  8. 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake

    题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...

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

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

随机推荐

  1. 微信公众号"赞赏"功能来了 觉得不错就给作者打个赏吧

    微信很早以前就开始测试“赞赏”功能了,只是官方还没出公告,近日腾讯科技就发了一篇题为 试试给微信公众号“赞赏” 的文章,算是一个回应吧.微信打赏功能势在遏制公众账号抄袭,鼓励用户创造优质内容,内容付费 ...

  2. Emag eht htiw Em Pleh(imitate)

    Emag eht htiw Em Pleh Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2901   Accepted:  ...

  3. Hadoop之 hdfs 系统

    一.NameNode维护着2张表: 1.文件系统的目录结构,以及元数据信息 2.文件与数据块列表的对应关系 存放在fsimage中,在运行的时候加载到内存中的. 操作日志写到edits中   二.Da ...

  4. 第15章 使用Postfix与Dovecot收发电子邮件

    章节概述: 本章节从电子邮局系统的组成角色开始讲起,了解MUA.MTA与MDA的作用,熟悉熟悉SMTP.POP3与IMAP4邮局协议. 学习postfix与dovecot服务程序的使用方法并逐条讲解配 ...

  5. 搭建DNS服务器

    导读 Linux下架设DNS服务器通常是使用Bind程序来实现的.Bind是一款实现DNS服务器的开放源码的软件.DNS即域名系统,主要功能是将人们易于记忆的Domain Name(域名)与不易记忆的 ...

  6. 关于 CAS 不能登录的问题

    经过排查,是因为 Capistrano 部署中设置了 http_proxy.此时通过 cas.m.xxxx.com 域名去访问 CAS 服务时,就不通了,需要修改为 IP 来访问. 但是公司的 CAS ...

  7. cocos2d调度器(定时执行某函数)

    调度器(scheduler) 继承关系 原理介绍 Cocos2d-x调度器为游戏提供定时事件和定时调用服务.所有Node对象都知道如何调度和取消调度事件,使用调度器有几个好处: 每当Node不再可见或 ...

  8. PXE介绍(PXE+kickstart无人值守安装)

    PXE概念 PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端 ...

  9. explict关键字

    [本文链接] http://www.cnblogs.com/hellogiser/p/explict.html [分析] explicit 只对构造函数起作用,用来抑制隐式转换. Suppose yo ...

  10. 转 MYSQL学习(一)

    第一期主要是学习MYSQL的基本语法,陆续还有第二.第三.第四期,大家敬请期待o(∩_∩)o 语法的差异 我这里主要说语法的不同 1.默认约束 区别:mysql里面DEFAULT关键字后面是不用加括号 ...