time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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 order.

After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted inlexicographical 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 siand 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 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

两两比较字符串。如果前一个串是当前串的前缀则无需重排字母表;如果当前串是前一个串的前缀则发生错误,Impossible;否则就找到最左不同字符,并形成两结点,令前一个字符结点指向当前字符结点,最后再做一次拓扑排序。

 /*
拓扑排序,跑一个队列
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
const int INF = 0x7fffffff;
const double EXP = 1e-;
const int MS = ;
const int cnt = ;
int n;
char str[MS][MS];
bool link[cnt][cnt];
int in[cnt]; //入度
char ans[cnt];
bool have;
void input()
{
cin >> n;
for (int i = ; i < n; i++)
cin >> str[i]; memset(in, , sizeof(in));
memset(link, false, sizeof(false));
have = true;
for (int i = ; i < n - &&have; i++)
{
int len1 = strlen(str[i]);
int len2 = strlen(str[i + ]);
int ok = true;
for (int j = ; j < len1&&j < len2&&ok; j++)
{
if (str[i][j] != str[i + ][j])
{
ok = false;
if (!link[str[i][j] - 'a'][str[i + ][j] - 'a'])
{
link[str[i][j] - 'a'][str[i + ][j] - 'a'] = true;
in[str[i + ][j] - 'a']++;
}
}
}
if (ok&&len1 > len2)
{
have = false;
}
}
} void solve()
{
if (!have)
{
cout << "Impossible" << endl;
return;
}
queue<int > que;
int num = ;
for (int i = ; i < cnt; i++)
if (in[i] == )
{
que.push(i);
ans[num++] = 'a' + i;
}
while (!que.empty())
{
int s = que.front();
que.pop();
for (int i = ; i < cnt; i++)
{
if (link[s][i])
{
in[i]--;
if (in[i] == )
{
ans[num++] = 'a' + i;
que.push(i);
}
}
}
}
if (num < cnt)
cout << "Impossible" << endl;
else
{
ans[num] = '\0';
cout << ans << endl;
}
} int main()
{
input();
solve();
return ;
}

C. Fox And Names的更多相关文章

  1. 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 ...

  2. CF Fox And Names (拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. codeforce 510C Fox And Names(拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. (CodeForces 510C) Fox And Names 拓扑排序

    题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...

  5. Fox And Names

    Description Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce ...

  6. [CF #290-C] Fox And Names (拓扑排序)

    题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...

  7. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

  8. codeforces 510 C Fox And Names【拓扑排序】

    题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入 ...

  9. CodeForces 510C Fox And Names (拓扑排序)

    <题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...

随机推荐

  1. 机器学习中的数学(4)-线性判别分析(LDA), 主成分分析(PCA)

    版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...

  2. ubuntu安装hudson

    因为hudson需要依赖java等,手动安装比较费劲 官方给出了一种很简单的解决方案:http://wiki.eclipse.org/Hudson-ci/Installing_Hudson_DEB s ...

  3. 8天玩转并行开发——第一天 Parallel的使用

    转自:http://www.cnblogs.com/huangxincheng/archive/2012/04/02/2429543.html 随着多核时代的到来,并行开发越来越展示出它的强大威力,像 ...

  4. 解决安装SQL Server2008失败的问题

    安装SQL Server2008时遇到"2008安装错误  必须重新启动计算机才能安装 SQL Server". 解决办法:HKEY_LOCAL_MACHINE\SYSTEM\Cu ...

  5. 关于H-Fox 函数

    ........We arrive at the following results which provide the sine and cosine transforms of the H-fun ...

  6. 在linux下编译netcat并且反弹cmdshell(转载)

    本地Windows监听 nc -vv -l -p 1234   首先从sf上get一个tar的压缩包 wget http://sourceforge.net/projects/netcat/files ...

  7. VMM服务模板(虚机、APP)部署排错

    I won't focus this blog on how to create a service template but more on how you can track the change ...

  8. Codeforces Round #115 B. Plane of Tanks: Pro 水题

    B. Plane of Tanks: Pro Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/17 ...

  9. 套题 Codeforces Round #277 (Div. 2)

    A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...

  10. java web servlet

    一.什么是Servlet Servlet是一种小型的Java程序,它扩展了Web服务器的功能.作为一种服务器端的应用,他是运行在Servlet容器当中,例如Tomcat就是一种流行的Servlet容器 ...