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: 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 si andti 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
题目给出一个字符串集,并假设是字典序的,要求26个字母的字典序(自定义的字典序),每两个字符串,可以得出一对字符的优先级,然后得出了所有字符的先后顺序,就转化成了拓扑排序的问题了,使用了优先队列,这样可以尽可能的小的在前面。其次,如果有ab a,这样的字符串,可以直接认为是不可能存在的!

#define N 105
#define MOD 1000000000000000007
struct node{
    int x;
    node(int xx){
        x = xx;
    }
    bool operator < (const node a) const{
        return x>a.x;
    }
};
int n,in[30],ans[30],ansNum;
char str[N][N];
bool land[30][30];
priority_queue<node> myqueue;
bool getland(int x,int y){
    int len = min(strlen(str[x]),strlen(str[y]));
    FI(len){
        if(str[x][i] != str[y][i]){
            land[str[x][i] - 'a'][str[y][i]-'a'] = true;
            return true;
        }
    }
    if(strlen(str[x])>strlen(str[y]))
    return false;
    return true;
}
int main()
{
    while(S(n)!=EOF)
    {
        FI(n){
            SS(str[i]);
        }
        memset(land,false,sizeof(land));
        bool flag = true;
        for(int i=0;i<n && flag;i++){
            for(int j = i+1;j<n && flag;j++){
                flag = getland(i,j);
            }
        }
        if(!flag){
            printf("Impossible\n");
            continue;
        }
        memset(in,0,sizeof(in));
        FI(26){
            FJ(26){
                if(land[i][j]){
                    in[j]++;
                }
            }
        }
        while(!myqueue.empty())
            myqueue.pop();
        for(int i=0;i<26;i++){
            if(in[i] == 0){
               myqueue.push(node(i));
            }
        }
        ansNum = 0;
        while(!myqueue.empty()){
            node top = myqueue.top();
            myqueue.pop();
            ans[ansNum++] = top.x;
            FI(26){
                if(land[top.x][i]){
                    in[i]--;
                    if(in[i] == 0)
                    myqueue.push(node(i));
                }
            }
        }
        if(ansNum == 26){
            FI(ansNum)
                printf("%c",ans[i]+'a');
            printf("\n");
        }
        else
            printf("Impossible\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-03 23:09:44

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

Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[],使得两个数组中最大的数尽量小 题解 按照偏序表,构造出从小到大的拓扑图 如何解决相等的数的偏序关系? 用并查集缩点后再进行拓扑排序 如何解决最大的数最小? 只需要使得同一层的数相同就行,可以一批处理栈中的元素,对于一批栈中的元素产生的新点,先放进一个容器里,然后等到这批栈清空了,再把这个容器中的点

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 #292 (Div. 1) B. Drazil and Tiles(拓扑排序)

题目地址:codeforces 292 B 用队列维护度数为1的点,也就是可以唯一确定的点,然后每次找v1,v2,并用v2来更新与之相连的点,如果更新后的点度数为1,就加入队列.若最后还有为"."的,说明无解或解不唯一. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #i

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupie

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) 解题报告 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

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 #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 510C) Fox And Names 拓扑排序

题目链接:http://codeforces.com/problemset/problem/510/C 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.