CodeForces - 510C Fox And Names

来源:互联网 发布:围棋少年 知乎 编辑:程序博客网 时间:2024/05/16 08:16

Fox And Names
Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Submit Status

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

Sample Input

Input
3rivestshamiradleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer
Output
Impossible
Input
10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck
Output
acbdefhijklmnogpqrstuvwxyz

Source

拓扑排序模板题,本身没什么好说的,但是刘汝佳在白书中用到一个技巧,就是一个数组可以表示结点的三种状态:0代表未被访问过,1代表已经访问过,-1代表正在访问。


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 110;
char s[N][N];
int wight[N][N], visit[N], dis[N];
int dfs(int u);
int tuopu();
int cnt;


int main()
{
    int n;
    while(scanf("%d", &n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
        }
        memset(wight,0,sizeof(wight));
        int flag=1;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;s[i][j]||s[i+1][j];j++)
            {
                if(s[i][j]=='\0')
                {
                    break;
                }
                if(s[i+1][j]=='\0')
                {
                    flag=0;
                    break;
                }
                if(s[i][j]!=s[i+1][j])
                {
                    int x=s[i][j]-'a'+1, y=s[i+1][j]-'a'+1;
                    wight[x][y]=1;
                    break;//注意
                }
            }
            if(flag==0)
            {
                break;
            }
        }
        if(flag==0)
        {
            printf("Impossible\n");
        }
        else
        {
            if(tuopu()==-1)
            {
                printf("Impossible\n");
            }
            else
            {
                for(int i=1;i<=26;i++)
                {
                    printf("%c",(dis[i]-1)+'a');
                }
                printf("\n");
            }
        }
    }
    return 0;
}


int tuopu()
{
    cnt=27;
    memset(visit,0,sizeof(visit));
    for(int i=1;i<=26;i++)
    {
        if(visit[i]==0)
        {
            if(dfs(i)==0)
            {
                return -1;
            }
        }
    }
    return 1;
}


int dfs(int u)
{
    visit[u]=-1;
    for(int i=1;i<=26;i++)
    {
        if(wight[u][i])
        {
            if(visit[i]<0)
            {
                return 0;
            }
            else if(visit[i]==0&&dfs(i)==0)
            {
                return 0;
            }
        }
    }
    visit[u]=1;
    dis[--cnt]=u;
    return 1;
}

0 0
原创粉丝点击