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. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  2. HDU5634 Rikka with Phi 线段树

    // HDU5634 Rikka with Phi 线段树 // 思路:操作1的时候,判断一下当前区间是不是每个数都相等,在每个数相等的区间上操作.相当于lazy,不必更新到底. #include & ...

  3. POJ 1003 解题报告

    1.问题描述: http://poj.org/problem?id=1003 2.解题思路: 最直观的的想法是看能不能够直接求出一个通项式,然后直接算就好了, 但是这样好水的样子,而且也不知道这个通项 ...

  4. 在linux上用dd命令实现ghost功能

    ghost和g4l 安装操作系统,速度太慢,整个过程太冗长乏味了. 安装过程中,需要回答若干问题,系统需要安装无数个软件,创建和写入无数的文件.因为涉及到大量的文件定位和读写,速度一定是快不起来的. ...

  5. google_protobuf数据类型

    要通信,必须有协议,否则双方无法理解对方的码流.在protobuf中,协议是由一系列的消息组成的.因此最重要的就是定义通信时使用到的消息格式. Protobuf消息定义 消息由至少一个字段组合而成,类 ...

  6. WebService学习之三:spring+cxf整合

    步骤一:spring项目(java web项目)引入CXF jar包 步骤二:创建webservice服务器 1)创建一个服务接口 package com.buss.app.login; import ...

  7. thymeleaf学习

    一.简单表达格式:   thymeleaf的官方参考文档 1.变量的表达式:${...} 2.选择变量表达式:*{...} 3.信息表达:#{...} 4.链接URL表达式:@{...} 二.字面值 ...

  8. CodeForces 707C Pythagorean Triples (数论)

    题意:给定一个数n,问你其他两边,能够组成直角三角形. 析:这是一个数论题. 如果 n 是奇数,那么那两边就是 (n*n-1)/2 和 (n*n+1)/2. 如果 n 是偶数,那么那两边就是 (n/2 ...

  9. pymol编译

    https://pymolwiki.org/index.php/Linux_Install

  10. Spring的ControllerAdvice注解

    @ControllerAdvice,是spring3.2提供的新注解,其实现如下所示: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUN ...