CodeForces - 510C Fox And Names
来源:互联网 发布:围棋少年 知乎 编辑:程序博客网 时间:2024/05/16 08:16
Description
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in thelexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: si ≠ ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters si and ti according to their order in alphabet.
Input
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
Output
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
Sample Input
3rivestshamiradleman
bcdefghijklmnopqrsatuvwxyz
10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer
Impossible
10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever
aghjlnopefikdmbcqrstuvwxyz
7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck
acbdefhijklmnogpqrstuvwxyz
Source
拓扑排序模板题,本身没什么好说的,但是刘汝佳在白书中用到一个技巧,就是一个数组可以表示结点的三种状态:0代表未被访问过,1代表已经访问过,-1代表正在访问。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 110;
char s[N][N];
int wight[N][N], visit[N], dis[N];
int dfs(int u);
int tuopu();
int cnt;
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%s",s[i]);
}
memset(wight,0,sizeof(wight));
int flag=1;
for(int i=0;i<n-1;i++)
{
for(int j=0;s[i][j]||s[i+1][j];j++)
{
if(s[i][j]=='\0')
{
break;
}
if(s[i+1][j]=='\0')
{
flag=0;
break;
}
if(s[i][j]!=s[i+1][j])
{
int x=s[i][j]-'a'+1, y=s[i+1][j]-'a'+1;
wight[x][y]=1;
break;//注意
}
}
if(flag==0)
{
break;
}
}
if(flag==0)
{
printf("Impossible\n");
}
else
{
if(tuopu()==-1)
{
printf("Impossible\n");
}
else
{
for(int i=1;i<=26;i++)
{
printf("%c",(dis[i]-1)+'a');
}
printf("\n");
}
}
}
return 0;
}
int tuopu()
{
cnt=27;
memset(visit,0,sizeof(visit));
for(int i=1;i<=26;i++)
{
if(visit[i]==0)
{
if(dfs(i)==0)
{
return -1;
}
}
}
return 1;
}
int dfs(int u)
{
visit[u]=-1;
for(int i=1;i<=26;i++)
{
if(wight[u][i])
{
if(visit[i]<0)
{
return 0;
}
else if(visit[i]==0&&dfs(i)==0)
{
return 0;
}
}
}
visit[u]=1;
dis[--cnt]=u;
return 1;
}
- codeforces--510C--Fox And Names
- Codeforces 510C - Fox And Names
- Fox And Names - CodeForces 510 C
- codeforces 510C Fox And Names 拓扑
- codeforces-510C-Fox And Names【DFS】
- CodeForces 510C Fox And Names
- CodeForces - 510C Fox And Names
- [拓扑] Codeforces #510C. Fox And Names
- codeforces 510C Fox And Names 拓扑排序
- Codeforces 510C - Fox And Names (拓扑排序)
- Codeforces 510C Fox And Names 拓扑排序
- codeforces 510c Fox And Names 拓扑排序
- [CodeForces 510C]Fox And Names[字典序][拓扑排序]
- Codeforces 510C Fox And Names 拓扑排序
- [CodeForces] 510 C Fox And Names [拓扑排序]
- 拓扑排序 CodeForces - 510C Fox And Names
- 【CodeForces】510C - Fox And Names(拓扑 & STL)
- Codeforces 510C Fox And Names【拓扑排序】
- iOS获取当前app的名称和版本号
- 飞驰在Mesos的涡轮引擎上
- 前端基础知识之CSS初识
- 2016年腾讯android开发工程师面试题目
- 正交矩阵相乘,范数不变性
- CodeForces - 510C Fox And Names
- #500-7 [编程作业]3_2 素数和
- FZU2150->BFS
- Android Studio---Error:(1, 0) Your project path contains non-ASCII characters.
- LeetCode 155 Min Stack (Java 容器 泛型)
- bootstrap datetimepicker 左右箭头不显示 icon-arrow-left
- 软件设计师——计算机与软件工程知识&学习要点(上午题B)
- 第1章 初识hadoop
- Class对象