Codeforces Round #290 (Div. 2)C

C. Fox And Names

  • 原题

Time Limit 2s

Memory Limit 256M

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 taccording 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).

SampleInput1

3
rivest
shamir
adleman

SampleOutput1

bcdefghijklmnopqrsatuvwxyz

SampleInput2

10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer

SampleOutput2

Impossible

SampleInput3

10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever

SampleOutput3

aghjlnopefikdmbcqrstuvwxyz

SampleInput4

7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck

SampleOutput4

acbdefhijklmnogpqrstuvwxyz

  • 题意

题意很明白,修改26个小写字母的字典序,使它满足读入的字符串按字典序排列。

  • Solution

根据读入的strings得出26个拉丁字母需要满足的拓扑序列,最终只需输出一种可能解即可。

问题是如何得到拓扑序列:

  1. 每次将当前字符串与前一个字符串进行逐位比较

  2. 一直找到一位不同的字符,说明后一个字符在修改后的字典序中比前一个字符来得大(所以就可以有拓扑了啊= =)

  3. 注意对字典序的定义('ab'<'abc')

  • Coddddde

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define N 105
#define C 26

using namespace std;

int len[N], gin[C], pr[C], qu[C], cnt, h, t;
char st[N][N];
vector <int> son[C];

void make(int x) {//拓扑
	pr[++cnt] = x;
	vector <int> :: iterator it;
	for (it = son[x].begin(); it != son[x].end(); it++) {
		gin[*it]--;
		if (gin[*it] == 0) {
			qu[t++] = *it;
		}
	}	
}

int main () {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		cin >> st[i];
		len[i] = strlen(st[i]);
	}
	for (int i = 2; i <= n; ++i) {
		int k = 0;
		while (k < len[i] && k < len[i - 1] && st[i][k] == st[i - 1][k]) {
			k++;
		}
		if (k == len[i] || k == len[i - 1]) {
			if (k < len[i - 1]) {//判断第3条的特殊情况
				printf("Impossible\n");
				return 0;
			}
			continue;
		}
		son[st[i - 1][k] - 'a'].push_back(st[i][k] - 'a');
		gin[st[i][k] - 'a']++;//入度增加
	}
	for (int i = 0; i < 26; ++i) {
		if (gin[i] == 0) qu[t++] = i;//初始化入度为0的进入队列
	}
	while (h < t) {
		make(qu[h]);
		h++;
	}
	if (h == 26) {//可行
		for (int i = 1; i <= cnt; ++i) {
			printf("%c", 'a' + pr[i]);
		}
		printf("\n");
	}
	else {//拓扑序列中有环
		printf("Impossible\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/jokingcoder/article/details/81132880