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

题目链接:http://codeforces.com/problemset/problem/510/C


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 and tiaccording 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


题意:

求是否存在一个字母表,能满足给出的字符串是从小到大给出的!若存在输出此字母表,

若不存在,输出Impossible!

PS:

两两比较字符串。若有两个字符串的前缀相同,前一字符串比后一字符串的长度长那么肯定是不合法的!也绝不会存在有合法的字母表了!

否则就找到最左不同字符,并形成两结点,令前一个字符结点指向当前字符结点,最后再做一次拓扑排序。

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int n;
char s[147][147];
int G[147][147];//连边
int in[147];//入度
char ans[147];
int k;
void toposort()//拓扑排序
{
    queue<int >q;
    k = 0;
    for(int i = 0; i < 26; i++)
    {
        if(!in[i])
        {
            q.push(i);
            ans[k++] = 'a' + i;
        }
    }
    while(!q.empty())
    {
        int f = q.front();
        q.pop();
        for(int i = 0; i < 26; i++)
        {
            if(G[f][i])//有相连的边
            {
                in[i]--;
                if(in[i] == 0)
                {
                    q.push(i);
                    ans[k++] = 'a' + i;
                }
            }
        }
    }
    if(k < 26)
    {
        printf("Impossible\n");
    }
    else
    {
        ans[k] = '\0';
        printf("%s\n",ans);
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        getchar();
        memset(in,0,sizeof(in));
        memset(G,0,sizeof(G));
        for(int i = 0; i < n; i++)
        {
            gets(s[i]);
        }
        int mark = 1;
        for(int i = 0; i < n-1 && mark; i++)
        {
            int len1 = strlen(s[i]);
            int len2 = strlen(s[i+1]);
            int flag = 0;
            for(int j = 0; j < len1 && j < len2; j++)
            {
                if(s[i][j] != s[i+1][j])
                {
                    if(!G[s[i][j]-'a'][s[i+1][j]-'a'])
                    {
                        G[s[i][j]-'a'][s[i+1][j]-'a'] = 1;
                        in[s[i+1][j]-'a']++;
                    }
                    flag = 1;
                    break;
                }
            }
            if(!flag && len1 > len2)//特判,前缀相同,且前一字符串币后一字符串还长,肯定不合法
            {
                mark = 0;
            }
        }
        if(!mark)
        {
            printf("Impossible\n");
            return 0;
        }
        toposort();
    }
    return 0;
}


你可能感兴趣的:(拓扑排序,codeforces,TopoSort)