拓扑排序 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 ...
随机推荐
- sql批量获取wordpress所有留言者的邮件地址
如果你的wordpress博客有很多读者互动的话,他们的留言都会留下具体的联系邮箱,我们如何批量导出这些联系信息呢?可以试试下面的sql语句 SELECT DISTINCT comment_autho ...
- HTML中的IE条件注释
IE条件注释是一种特殊的HTML注释,这种注释只有IE5.0及以上版本才能理解.比如普通的HTML注释是: <!--This is a comment--> 而只有IE可读的IE条件注释是 ...
- sqlmap如何修改线程
找到settings.py文件,具体在\lib\core\目录下找到 # Maximum number of threads (avoiding connection issues and/or Do ...
- 技术分享:WIFI钓鱼的入门姿势
简介 该实验先是搭建一个测试环境,然后创建一个假的无线接入点,把网络连接连接到假的接入点并且强迫用户连接假的无线点. 事先准备 1.无线网卡:无线网卡用于数据包的嗅探和注入. 2. Backtrack ...
- HDOJ 1869
#include<stdio.h> ][]; #define inf 0xffffff; void floyed(int n) { int i,j,k; ;k<n;k++) { ;i ...
- 【转】android中Uri.parse()用法
1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = new Intent(Intent.ACTI ...
- Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 关于https和数字证书的一些必须知识
下面这篇文章叫做 HTTPS连接的前几毫秒发生了什么 ,是一篇译文,写得不错,十分有助于理解https. http://blog.jobbole.com/48369/ 下面的链接是百度文库的 数字证 ...
- iOS category中的所谓属性 和 从xib初始化对象的方法 以及类扩展
今天在编码时遇到以下代码 @interface UITextField (TCCustomFont) @property (nonatomic, copy) NSString* fontName; @ ...
- Heap:Sunscreen(POJ 3614)
晒太阳 题目大意:一堆牛,为了避免晒太阳会灼烧自己,然后他们自己有自己的防晒指数(一个区间),防晒霜可以提高防晒因数SPF,大了不行小了不行,现在有一桶防晒霜,他们提供一定的SPF,但是最多可以提供k ...