拓扑排序 CodeForces - 510C Fox And Names

题目

Description

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 thelexicographical 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 becomeslexicographical!

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 thelexicographical order. If so, you should find out any such order.

Lexicographical order is defined in following way. When we compares 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 characterssi 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 stringnamei (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 Input

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

Sample Output

Hint

Source


题目大意是给出一个排列好的序列,问存不存在一种特殊的字典序,使得这样的排列成立,存在就输出。
拓扑排序,我虽然高中学过原理,却从来没有自己亲自写过。这可以说是我第一次写。
有一个坑点是,假如g[i][j]赋值为1时,没判定原来g[i][j]是否已经为1,就让入度(i) + 1了,就不对了。不能出现重复的情况。
这里用的是kahn算法。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;
int n,status=1;
char temp1[110],temp2[110];
int a[30][30],indegree[30],b[30],bnum=0;
queue <int> q;
int zhuanhuan(char x)
{
    return(x-96);
}
void paint(void)
{
    int str1,str2,str,statusn=0;
    str1=strlen(temp1);
    str2=strlen(temp2);
    str=min(str1,str2);
    for(int i=0;i<str;i++)
    {
        int x1=zhuanhuan(temp1[i]);
        int x2=zhuanhuan(temp2[i]);
        if(x1!=x2)
        {
            statusn=1;
            if(a[x1][x2]==1)
            {
                break;
            }
            a[x1][x2]=1;
            indegree[x2]++;
            //printf("%c - %c\n",temp1[i],temp2[i]);
            break;
        }
    }
    if(statusn==0)
    {
        if(str1>str2)
        {
            status=0;
        }
    }
}

void kahn(void)
{
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        b[bnum++]=x;
        for(int j=1;j<=26;j++)
        {
            if(a[x][j]==1)
            {
                a[x][j]=0;
                indegree[j]--;
                if(indegree[j]==0)
                    q.push(j);
            }
        }
    }
    for(int i=1;i<=26;i++)
    for(int j=1;j<=26;j++)
    {
        if(a[i][j]>0)
            status=0;
    }
}
int main(void)
{
    memset(a,0,sizeof(a));
    memset(indegree,0,sizeof(indegree));
    status=1;
    scanf("%d",&n);
    scanf("%s",temp1);
    for(int i=1;i<n;i++)
    {
        scanf("%s",temp2);
        paint();
        int j;
        for(j=0;j<strlen(temp2);j++)
            temp1[j]=temp2[j];
        temp1[j]='\0';
    }
    for(int i=1;i<=26;i++)
        if(indegree[i]==0)
            q.push(i);
    kahn();
    if(status==0)
        printf("Impossible");
    else
    {
        for(int i=0;i<bnum;i++)
            printf("%c",b[i]+96);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值