拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names
/*
给出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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...
- DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots
题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...
- 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake
题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...
- Codeforces Round #290 (Div. 2) 拓扑排序
C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- jquery 调用数据
<body> <div id="aa" style="">hello</div> <div class="b ...
- Unity3d发布成exe项目后的设置(全屏自适应屏幕大小)
原地址:http://blog.sina.com.cn/s/blog_697b1b8c0101gd46.html 1. 去除启动exe项目时出现的画面窗口 File ☞ Build Settings ...
- gem install factory_girl
文章是从个人博客转过来的, 可以直接访问 iwangzheng.com https://github.com/thoughtbot/factory_girl https://github.com/t ...
- python4delphi 设置syspath
详细看demo25的代码 These techniques are demonstrated in Demo25 in the examples folder of your Python for D ...
- How to install OpenResty
How to install OpenResty 15 January 2014, 6:18 am OpenResty, also called “ngx_openresty”, is a web ...
- 学习jquery mobile
学习jquery mobile的时间不是很长,在学习的过程当中也遇到了很多令人抓狂的问题,在网上搜索问题答案的时候发现,现在关于jquery mobile的文章还不是很多,所以,我也是一边学习,一边摸 ...
- DOS命令符基本操作
怎么改变DOS默认路径 开始->所有程序->附件->命令提示符,在“命令提示符”上右键,选择“属性”,(或者在快捷方式上点击属性)会弹出一个“命令提示符 属性”对话框,可以通过修改该 ...
- Linux系统排查3——I/O篇
当磁盘无法写入的时候,一般有以下可能: 文件系统只读 磁盘已满 I节点使用完 一. 遇到只读的文件系统 文件系统自动设置成只读可能是系统自我保护的一种机制,因此需要实现弄清究竟是什么原因造成了文件系统 ...
- 项目总结(五)--- 界面调试工具Reveal
在开发中,我们也许会碰到以下需求:对于一些动态复杂的交互界面,手码去制定界面是常有的事情,然而我们在开发中想修改过一些参数后想看下实时效果,只能重新运行项目,进入到对应的页面来进行修改,是不是有点麻烦 ...
- java操作AJAX
1,get方式的AJAX function sendAjaxReq() { //1,创建ajax引擎 XMLHttpRequest对象 var req = new XMLHttpRequest() | ...