Fox And Names

     
D - D
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 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 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
这个题的意思是说,按照字符串的输入顺序重新编排字典序。前一个字符串与后一个字符串相比较,只要出现不同的了就把前一个字符串的字母指向后一个字符串的字母,如果符合情况的话就用拓扑排序把重新编排后的26个字母输出,否则输出
Impossible
#include <stdio.h>
#include <string.h>
char st[1000][1000];
int map[100][100];
int num[100],queue[100];
void toposort()
{
    int i,j,x,c=0;
    for(i=0; i<26; i++)
    {
        int f=1;
        for(j=0; j<26; j++)
        {
            if(num[j]==0)
            {
                num[j]=-1;
                x=j;
                f=0;
                break;
            }
        }
        if(f)               //这种情况是判断目前是否已成环,如果成环了则没有入度为0的点了
            break;
        queue[c++]=x;
        for(j=0; j<26; j++)
        {
            if(map[x][j]==1)
            {
                map[x][j]=0;
                num[j]--;
            }
        }
    }
    if(c<26)
        printf("Impossible\n");
    else
    {
        for(i=0; i<c; i++)
        {
            printf("%c",queue[i]+'a');
        }
        printf("\n");
    }
}
int main()
{
    int n,i,j,x,y;
    while(~scanf("%d",&n))
    {
        memset(map,0,sizeof(map));        //记得清零
        memset(num,0,sizeof(num));
        memset(queue,0,sizeof(queue));
        int flag=0;
        for(i=0; i<n; i++)
            scanf("%s",st[i]);
        for(i=1; i<n; i++)
        {
            int len1=strlen(st[i-1]);
            int len2=strlen(st[i]);
            if(len1>len2)
            {
                for(j=0; st[i-1][j]!='\0'; j++)
                {
                    if(j==len2)
                    {
                        flag=1;              //用来判断(第一行aaa 第二行a)这种情况的
                        break;
                    }
                    if(st[i-1][j]!=st[i][j])
                    {
                        x=st[i-1][j]-'a';
                        y=st[i][j]-'a';
                        if(!map[x][y])       //开始构建图
                        {
                            map[x][y]=1;
                            num[y]++;
                        }
                        break;
                    }
                }
            }
            else if(len1<len2)
            {
                for(j=0; st[i][j]!='\0'; j++)
                {
                    if(j==len1)
                        break;
                    if(st[i-1][j]!=st[i][j])
                    {
                        x=st[i-1][j]-'a';
                        y=st[i][j]-'a';
                        if(!map[x][y])
                        {
                            map[x][y]=1;
                            num[y]++;
                        }
                        break;
                    }
                }
            }
            else
            {
                for(j=0; st[i][j]!='\0'; j++)
                {
                    if(st[i-1][j]!=st[i][j])
                    {
                        x=st[i-1][j]-'a';
                        y=st[i][j]-'a';
                        if(!map[x][y])
                        {
                            map[x][y]=1;
                            num[y]++;
                            
                        }
                        break;
                    }
                }
            }
        }
        if(flag==1)
        {
            printf("Impossible\n");
            continue;
        }
        toposort();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值