CF290-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

拓扑排序

给定n个字符串,要求构造一个字母顺序表以至于这n个字符串是按照字典顺序排序好的(两个子串比较大小从最低位开始,字符大的的字符串大,如b>ab)
首先题目只要求构造一个顺序,我们需要只依照给定的n个字符串的字母相对顺序和原字母表的顺序就可以确定新的顺序.这样很容易就想到拓扑排序
我们默认从1~n的字符串已经是有序的了.
依次比较相邻的两个字符串,将不满足大小顺序的字母在拓扑图上表示
如代码中的:indegree[k2]++; graph[k1].push_back(k2);意味着k1顺序应该在k2之前
遍历所有的字符串后意味着顺序已经确定好,输出就可以了
#include <iostream>
#include <string.h>
#include <vector>

int indegree[200];
int use[200];
using namespace std;
vector<int> graph[200];
vector<int> res;
void topsort()
{
    while(true)
    {
        for(int i=0;i<26;i++)
        {
            if(indegree[i]==0&&graph[i].size()==0&&!use[i])
            {
                use[i]=1;
                res.push_back(i);
            }
        }
        int k=0;
        for(;k<26;k++)
        {
            if(indegree[k]==0&&!use[k])
                break;
        }
        if(k<26)
        {
            use[k]=1;
            res.push_back(k);
            for(int i=0;i<graph[k].size();i++)
            {
                indegree[graph[k].at(i)]--;
            }
            graph[k].clear();
        }
        else
        {
            if(res.size()==26)
                break;
            cout<<"Impossible"<<endl;
            return ;
        }
    }
    for(int i=0;i<res.size();i++)
    {
        cout<<char(res.at(i)+'a');
    }
    cout<<endl;
}
int main()
{
    int n;
    memset(use,0,sizeof(use));
    memset(indegree,0,sizeof(indegree));
    char str[200][200];
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>str[i];
    }
    for(int i=2;i<=n;i++)
    {
        char s1[200];
        char s2[200];
        strcpy(s1,str[i-1]);
        strcpy(s2,str[i]);
        int k1=-1;
        int k2=-1;
        for(int j=0;j<min(strlen(s1),strlen(s2));j++)
        {
            if(s1[j]==s2[j])
            {
                continue;
            }
            else
            {
                k1=s1[j]-'a';
                k2=s2[j]-'a';
                break;
            }
        }
        if(k1==-1)
        {
            if(strlen(s1)>strlen(s2))
            {
                cout<<"Impossible"<<endl;
                return 0;
            }
            else
                continue;
        }
        indegree[k2]++;
        graph[k1].push_back(k2);
    }
    topsort();
    return 0;
}

 

posted @ 2015-02-11 19:19  伟gg  阅读(270)  评论(0编辑  收藏  举报