Codeforces Round #290 (Div. 2) 拓扑排序

C. Fox And Names

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

Examples

input

3rivestshamiradleman

output

bcdefghijklmnopqrsatuvwxyz

input

10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer

output

Impossible

input

10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever

output

aghjlnopefikdmbcqrstuvwxyz

input

7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck

output

acbdefhijklmnogpqrstuvwxyz

题意:给你n个字符串 现在要求你重新定义字典序 使得n个字符串的顺序为升序  输出新的26个字母的字典序题解:对相邻的两个字符串 在第一个字符不同的位置的两个字符间连一条单向边 拓扑排序
 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <bits/stdc++.h>
 3 #include <cstdlib>
 4 #include <cstdio>
 5 #include <iostream>
 6 #include <cstdlib>
 7 #include <cstring>
 8 #include <algorithm>
 9 #include <cmath>
10 #include <cctype>
11 #include <map>
12 #include <set>
13 #include <queue>
14 #include <bitset>
15 #include <string>
16 #include <complex>
17 #define LL long long
18 #define mod 1000000007
19 using namespace std;
20 char a[105][1003];
21 int g[26][26];
22 int in[26];
23 queue<int> q;
24 int vis[26];
25 int ans[26];
26 int jishu=0;
27 int n;
28 bool topsort(){
29     while(!q.empty())
30         q.pop();
31     for(int i=0;i<=25;i++){
32         if(in[i]==0){
33         q.push(i);
34         vis[i]=1;
35         }
36     }
37     jishu=0;
38     int exm;
39     while(!q.empty()){
40         exm=q.front();
41         ans[jishu++]=exm;
42         q.pop();
43         for(int i=0;i<=25;i++){
44             if(g[exm][i]==1){
45                 if(--in[i]==0&&vis[i]==0){
46                     vis[i]=1;
47                     q.push(i);
48                 }
49             }
50         }
51     }
52 }
53 int main()
54 {
55     scanf("%d",&n);
56     for(int i=0;i<n;i++)
57         scanf("%s",a[i]);
58     for(int i=0;i<n-1;i++)
59     {
60         int l1,l2;
61         l1=strlen(a[i]);
62         l2=strlen(a[i+1]);
63         int p=0;
64         while(p<min(l1,l2)&&a[i][p]==a[i+1][p]) p++;
65         if(p==l1&&l1<l2) continue;
66         if(p==l2&&l2<l1) {
67             printf("Impossible\n");//**
68             return 0;
69         }
70         if(g[a[i][p]-‘a‘][a[i+1][p]-‘a‘]) continue;
71         g[a[i][p]-‘a‘][a[i+1][p]-‘a‘]=1;
72         in[a[i+1][p]-‘a‘]++;
73     }
74     topsort();
75     if(jishu==26){
76         for(int i=0;i<=25;i++)
77             printf("%c",ans[i]+‘a‘);
78     }
79     else
80         printf("Impossible\n");
81     return 0;
82 }
时间: 2024-12-28 21:22:34

Codeforces Round #290 (Div. 2) 拓扑排序的相关文章

Codeforces Round #290 (Div. 2) b

/** * @brief Codeforces Round #290 (Div. 2) b * @file a.cpp * @author mianma * @created 2015/02/04 15:17 * @edited 2015/02/04 15:17 * @type brute * @note */ #include <fstream> #include <iostream> #include <string> #include <cstring>

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

C. Fox And Names 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: t

Codeforces Round #290 (Div. 2) 解题报告 A.B.C.D.

A - Fox And Snake 模拟. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h> using na

Codeforces Round #290 (Div. 2) B (dfs)

题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相同且已搜过,则存在满足题意的环 代码: 1 #include <bits/stdc++.h> 2 #define MAXN 60 3 using namespace std; 4 5 int mp[MAXN][MAXN], vis[MAXN][MAXN], m, n; 6 int dir[4][2

Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring" int r,c; char map[55][55]; int vis[55][55]; int mark; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int judge(int x,int y) { if(x<0||x>=r||y<0||y>

Codeforces Round #290 (Div. 2)

A题: 简单的模拟. 贴样例就知道了. input 3 3 output ###..#### input 3 4 output ####...##### input 5 3 output ###..#####..### input 9 9 output #########........###########........#########........###########........######### 1 #include<cstdio> 2 int main() 3 { 4 in

B. Fox And Two Dots Codeforces Round #290 (Div. 2)

B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n?×?m ce

C. Fox And Names Codeforces Round #290 (Div. 2)

C. Fox And Names 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: t

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market 二分答案 +排序

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market 二分答案 +排序 题意 有 a[ i ] 个数 要求选最多的数 使其和不超过 S ,且在此情况下,和最小选最多数情况下 和最小 且 每个数有加成 如果选了 k个数 那么加成后 就是 a[ i ] + k*i ; 题解 二分mid 表示选了个数 加成一下,将加成以后结果排序一下 , 若前 mid数 和大于 s 则此方案不可行 PS 要用 long long ..... 还有 co