CodeForces510 C.Fox And Names (拓扑排序)

C.Fox And Names

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

Examples
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

思路:

找相邻串第一个不同的字母,假设第一个不同的位置为p,
上一个串的p对应字母对当前串p位置对应字母建立有向边,
因为是求大小关系,可以用拓扑排序
建完图之后跑拓扑排序得出答案

无解的条件:
上一个字符串必定比下面的一个大的时候,
即下面的串是上面串的前缀,且上面的串长度比下面长

这题的几个细节:
1.建边的时候要先判断是否已经建过边了,否则会重边,重边的影响是出点的入度变大。
2.因为要先判断是否已经建过边,所以不能用vector或者链式前向星存,要开二维数组
3.如果最后答案序列的大小不为26,说明存在拓扑排序冲突,此时无解

代码带少量注释

code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
const int maxm=105;
string s[maxm];
int sz[maxm];
int g[26][26];
int in[26];
int n;
void add(int a,int b){//添加一条a->b的有向边
    a-='a';
    b-='a';
    if(!g[a][b]){//先判断边是否已经存在,否则可能重边,如果重边入度会计算错误
        g[a][b]=1;
        in[b]++;
    }
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s[i];
        sz[i]=s[i].size();//长度
    }
    for(int i=2;i<=n;i++){
        int len=min(sz[i],sz[i-1]);
        int pos=-1;
        for(int j=0;j<len;j++){
            if(s[i][j]!=s[i-1][j]){
                pos=j;//标记第一个不同的位置
                break;
            }
        }
        if(pos==-1&&sz[i-1]>sz[i]){//如果上一个串必比当前的大,则无解
            cout<<"Impossible"<<endl;
            return 0;
        }else if(pos!=-1){//如果有不同的
            add(s[i-1][pos],s[i][pos]);//尝试建立有向边
        }
    }
    queue<int>q;
    for(int i=0;i<26;i++){
        if(in[i]==0){
            q.push(i);
        }
    }
    vector<int>ans;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        ans.push_back(x);
        for(int i=0;i<26;i++){
            if(g[x][i]){
                in[i]--;
                if(in[i]==0){
                    q.push(i);
                }
            }
        }
    }
    if(ans.size()!=26){//如果没有26个数说明某些字母冲突了(拓扑排序基本原理)
        cout<<"Impossible"<<endl;
        return 0;
    }
    for(int i=0;i<26;i++){
        cout<<(char)(ans[i]+'a');
    }
    cout<<endl;
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值