Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序

原创 2015年07月06日 21:41:05
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 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 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
题目给出一个字符串集,并假设是字典序的,要求26个字母的字典序(自定义的字典序),每两个字符串,可以得出一对字符的优先级,然后得出了所有字符的先后顺序,就转化成了拓扑排序的问题了,使用了优先队列,这样可以尽可能的小的在前面。其次,如果有ab a,这样的字符串,可以直接认为是不可能存在的!

#define N 105
#define MOD 1000000000000000007
struct node{
    int x;
    node(int xx){
        x = xx;
    }
    bool operator < (const node a) const{
        return x>a.x;
    }
};
int n,in[30],ans[30],ansNum;
char str[N][N];
bool land[30][30];
priority_queue<node> myqueue;
bool getland(int x,int y){
    int len = min(strlen(str[x]),strlen(str[y]));
    FI(len){
        if(str[x][i] != str[y][i]){
            land[str[x][i] - 'a'][str[y][i]-'a'] = true;
            return true;
        }
    }
    if(strlen(str[x])>strlen(str[y]))
    return false;
    return true;
}
int main()
{
    while(S(n)!=EOF)
    {
        FI(n){
            SS(str[i]);
        }
        memset(land,false,sizeof(land));
        bool flag = true;
        for(int i=0;i<n && flag;i++){
            for(int j = i+1;j<n && flag;j++){
                flag = getland(i,j);
            }
        }
        if(!flag){
            printf("Impossible\n");
            continue;
        }
        memset(in,0,sizeof(in));
        FI(26){
            FJ(26){
                if(land[i][j]){
                    in[j]++;
                }
            }
        }
        while(!myqueue.empty())
            myqueue.pop();
        for(int i=0;i<26;i++){
            if(in[i] == 0){
               myqueue.push(node(i));
            }
        }
        ansNum = 0;
        while(!myqueue.empty()){
            node top = myqueue.top();
            myqueue.pop();
            ans[ansNum++] = top.x;
            FI(26){
                if(land[top.x][i]){
                    in[i]--;
                    if(in[i] == 0)
                    myqueue.push(node(i));
                }
            }
        }
        if(ansNum == 26){
            FI(ansNum)
                printf("%c",ans[i]+'a');
            printf("\n");
        }
        else
            printf("Impossible\n");
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序

思路:对相邻两行,当s1[i]≠s2[i]时,新的字母表中s1[i]必须出现在s2[i]的前面,显然是拓扑排序。所以枚举相邻的两行,构造有向边统计入度。 拓扑排序: 每次从图中拿走一个入度为0的节点,...
  • ww32zz
  • ww32zz
  • 2015年08月03日 15:28
  • 288

Codeforces Round #290 (Div. 2) - C. Fox And Names(最短路)

题目: http://codeforces.com/problemset/problem/545/E 题意: n个点m条边的无向图,求出从1->n的最短路,使得其路径上标记为0的路径最少,除了这条最...

C. Fox And Names Codeforces Round #290 (Div. 2)

题意:给n个字符串,它们按照某个字典序从小到大排列,问这个字典序是否存在,存在就输出任意一个满足条件的字典序,否则输出“Impossible”。 裸的topsort,结果在终判时挂了,就因为没有特判,...

Codeforces 510C - Fox And Names (拓扑排序)

RT

Codeforces 510C Fox And Names 拓扑排序

传送门:510C C. Fox And Names time limit per test 2 seconds memory limit per test 2...

Codeforces 510C Fox And Names【拓扑排序】

Fox And Names time limit per test  2 seconds memory limit per test  256 megabytes i...

codeforces510C. Fox And Names【拓扑排序】

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes ...

codeforces 510C Fox And Names 拓扑排序

传送门:cf 510D 给定n个字符串,问能否存在这样的字母表,使得字符串的排序满足字典序。即依据新的字母表,排序满足字典序大小。 假设满足字典序,则我们可以依据已有的字符串得出各字母之间...

Codeforces Round #290 (Div. 1) A. Fox And Names

建出图,按入度进行广搜就行。。。。 #include #include #include #include #include #include #include #include #...

Codeforces Round #290 (Div. 1)A. Fox And Names

A. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序
举报原因:
原因补充:

(最多只允许输入30个字)