您的位置:首页 > 其它

CodeForces 510C (拓扑排序)

2016-08-14 17:46 459 查看
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 the lexicographical 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

Input

3

rivest

shamir

adleman

Output

bcdefghijklmnopqrsatuvwxyz

Input

10

tourist

petr

wjmzbmr

yeputons

vepifanov

scottwu

oooooooooooooooo

subscriber

rowdark

tankengineer

Output

Impossible

Input

10

petr

egor

endagorion

feferivan

ilovetanyaromanova

kostka

dmitriyh

maratsnowbear

bredorjaguarturnik

cgyforever

Output

aghjlnopefikdmbcqrstuvwxyz

Input

7

car

care

careful

carefully

becarefuldontforgetsomething

otherwiseyouwillbehacked

goodluck

Output

acbdefhijklmnogpqrstuvwxyz

这个题呢,英语不好还真是做不出来诶,像我这样的英语渣渣还是请了尊神才懂了题意,为了后面人不至于那么辛苦,我就翻译一下

题意就是给你 n 个字符串,让你比较前一个字符串与后一个字符串,比如 aaa f ed 与 aaa e sa 一个一个对比,相同的话就往后面比,找到不同的那对,就设定前面的字符串的那个字母要比后面的字典序要靠前,意思是原有字典序是 abcd ef…xyz 输出的,而现在就是abcd fe…xyz输出,依次向后面找,要注意只能有26个英文字母,不能重复,重复就输出Impossible

至于题怎么解呢,就是拓扑排序了,也算是模板题吧

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define M 110

char map[M][M], ans[27];
int in[M];
bool ok, link_map[27][27];

void topo()//拓扑排序
{
priority_queue< int , vector<int>, greater<int> > q;//从小到大排
for(int i=0; i<26; i++)
{
if(!in[i])
{
q.push(i);
}
}
int flag = 0;
int k = 0;
while(!q.empty())
{
flag = q.top();
q.pop();
ans[k++] = flag + 'a';
for(int i=0; i<26; i++)
{
if(link_map[flag][i])
{
in[i]--;
if(!in[i])
{
q.push(i);
}
}
}
}
if(k < 26)  ok = true;//陷入死循环的那种
ans[26] = '\0';
}

int main()
{
int n;
scanf("%d", &n);

for(int i=1; i<=n; i++)
{
scanf("%s", map[i]);
}
ok = false;
memset(in, 0, sizeof(in));
memset(link_map, false, sizeof(link_map));
for(int i=1; i<n && !ok; i++)
{
for(int j=0; j<M; j++)
{
if(map[i][j] == '\0')   break;//前面的结束了就是结束了
if(map[i+1][j] == '\0')//前面的没有结束后面的却结束了,就错了
{
ok = true;
break;
}
if(map[i][j] == map[i+1][j])//相同就继续
continue;
if(link_map[map[i][j] - 'a'][map[i+1][j] - 'a'])//连接过的就不再比了
break;
link_map[map[i][j] - 'a'][map[i+1][j] - 'a'] = true;//连接
in[map[i+1][j] - 'a']++;//入度加 1
break;
}
}
topo();
if(ok)
printf("Impossible\n");
else
{
printf("%s", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: