A. Fox And Names
time limit per test
memory limit per test
input
output
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
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
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
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 test(s)
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
拓扑排序即可,注意前一个串包含后一个串的情况
/*************************************************************************
> File Name: cf290a.cpp
> Author: ALex
> Created Time: 2015年02月03日 星期二 01时03分58秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
int ord[33];
char mat[110][110];
int edge[110][110];
int in[33];
vector <int> s;
bool vis[33];
char ans[33];
void topo ()
{
bool flag = 1;
int ret = 0;
for (int i = 1; i <= 26; ++i)
{
ret += vis[i];
}
memset (in, 0, sizeof(in));
s.clear();
int cnt = 1;
queue <int> qu;
while (!qu.empty())
{
qu.pop();
}
int x = 0;
for (int i = 1; i <= 26; ++i)
{
if (!vis[i])
{
continue;
}
int t = 0;
for (int j = 1; j <= 26; ++j)
{
t += edge[j][i];
}
in[i] = t;
if (!t)
{
qu.push (i);
++x;
s.push_back (i);
}
}
while (!qu.empty())
{
int u = qu.front();
qu.pop();
for (int i = 1; i <= 26; ++i)
{
if (!edge[u][i])
{
continue;
}
edge[u][i] = 0;
in[i]--;
if (in[i] == 0)
{
++x;
qu.push (i);
s.push_back (i);
}
}
}
if (x < ret)
{
printf("Impossible\n");
return;
}
x = 0;
memset (vis, 0, sizeof(vis));
for (int i = 0; i < s.size(); ++i)
{
ans[x++] = s[i] - 1 + 'a';
vis[s[i]] = 1;
// printf("%c\n", s[i] + 'a' - 1);
}
for (int i = 1; i <= 26; ++i)
{
if (!vis[i])
{
vis[i] = 1;
ans[x++] = i - 1 + 'a';
}
}
for (int i = x - 1; i >= 0; --i)
{
printf("%c", ans[i]);
}
printf("\n");
}
int main ()
{
int n;
while (~scanf("%d", &n))
{
memset (vis, 0, sizeof(vis));
memset (edge, 0, sizeof(edge));
for (int i = 1; i <= n; ++i)
{
scanf("%s", mat[i]);
}
bool x = true;
for (int i = 2; i <= n; ++i)
{
int len1 = strlen (mat[i - 1]);
int len2 = strlen (mat[i]);
bool flag = false;
char s, b;
for (int j = 0; j < min (len1, len2); ++j)
{
if (mat[i][j] == mat[i - 1][j])
{
continue;
}
else
{
s = mat[i - 1][j];
b = mat[i][j];
flag = true;
break;
}
}
if (!flag && len1 > len2)
{
x = false;
break;
}
else if (flag)
{
if (edge[b - 'a' + 1][s - 'a' + 1] == 0)
{
edge[b - 'a' + 1][s - 'a' + 1] = 1;
vis[b - 'a' + 1] = 1;
vis[s - 'a' + 1] = 1;
}
else if (edge[s - 'a' + 1][b - 'a' + 1])
{
x = false;
break;
}
}
}
if (!x)
{
printf("Impossible\n");
continue;
}
topo();
}
return 0;
}