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 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 test(s)
input
3rivestshamiradleman
output
bcdefghijklmnopqrsatuvwxyz
input
10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer
output
Impossible
input
10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever
output
aghjlnopefikdmbcqrstuvwxyz
input
7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck
output
acbdefhijklmnogpqrstuvwxyz

拓扑排序第一题,注意特判一下下面的串是上面的串的前缀这种情况,直接输出Impossible了。

 #include <bits/stdc++.h>
 using    namespace    std;

 ][];
 ];
 ];

 void    toposort(void);
 int    main(void)
 {
     int    n;
     ][];
     bool    flag;

     cin >> n;
     ;i < n;i ++)
         cin >> name[i];
     ;i < n - ;i ++)
     {
         ;name[i][j] && name[i + ][j];j ++)
         {
             flag = false;
             ][j])
             {
                 ][j] - 'a'])
                 {
                     G[name[i][j] - ][j] - 'a'] = true;
                     IN[name[i + ][j] - 'a'] ++;
                 }
                 flag = true;
                 break;
             }
         }
         ]))
         {
             puts("Impossible");
             ;
         }
     }
     toposort();

     ;
 }

 void    toposort(void)
 {
     queue<int>    que;
     ;
     ;
     int    front;

     ;i < ;i ++)
         if(!IN[i])
         {
             ANS[k ++] = i;
             sum ++;
             que.push(i);
         }

     while(!que.empty())
     {
         front = que.front();
         que.pop();
         ;i < ;i ++)
             if(G[front][i])
             {
                 IN[i] --;
                 if(!IN[i])
                 {
                     ANS[k ++] = i;
                     que.push(i);
                     sum ++;
                 }
             }
     }
     )
         puts("Impossible");
     else
     {
         ;i < ;i ++)
             printf("%c",ANS[i] + 'a');
         puts("");
     }

     return    ;
 }

CF Fox And Names (拓扑排序)的更多相关文章

  1. [CF #290-C] Fox And Names (拓扑排序)

    题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...

  2. (CodeForces 510C) Fox And Names 拓扑排序

    题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...

  3. CF 213A Game(拓扑排序)

    传送门 Description Furik and Rubik love playing computer games. Furik has recently found a new game tha ...

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

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

  5. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

  6. C. Fox And Names

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

  7. Fox And Names

    Description Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce ...

  8. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

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

    C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...

随机推荐

  1. R自动数据收集第二章HTML笔记1(主要关于handler处理器函数和帮助文档所有示例)

    本文知识点:     1潜在畸形页面使用htmlTreeParse函数 2startElement的用法 3闭包 4handler函数的命令和函数体主要写法 5节点的丢弃,取出,取出标签名称.属性.属 ...

  2. linux文件特殊权限

  3. PowerDesigner中表名过长,自动生成的主键名截取的问题

    在PowerDesinger中,若表名过长,自动生成的主键名会被自动截取. 解决如下:DataBase/Edit Current DBMS/Scripts/Objects/PKey/ConstName ...

  4. 图论$\cdot$强连通分量

    和无向图的连通分量类似,有向图有“强连通分量”的说法.“相互可达”的关系在有向图中也是等价关系.每一个集合称为有向图的一个强连通分量(scc).如果把一个集合看成一个点,那么所有的scc构成了一个sc ...

  5. JVM 参数翻译汉化解释

    博客搬家,新地址:http://www.zicheng.net/article/38.htm Behavioral Options(行为参数) Option and Default Value Des ...

  6. AngularJS的学习--TodoMVC的分析

    最近一段时间一直在看AngularJS,趁着一点时间总结一下. 官网地址:http://angularjs.org/ 先推荐几个教程 1. AngularJS入门教程 比较基础,是官方Tutorial ...

  7. OUYA游戏开发核心技术剖析大学霸内部资料

    OUYA游戏开发核心技术剖析大学霸内部资料 试读地址:http://pan.baidu.com/s/1ntuql8t 介绍:本教程是一本进阶级的教材,它可以让读者在了解.熟悉了OUYA设备的基础上,开 ...

  8. FTP搭建

    在桌面上右击“我的电脑”,执行“管理”命令,在“计算机管理”窗口的左窗格中依次展开“系统工具 ”→“本地用户和组”目录,单击选中“用户”选项.在右侧窗格中单击右键,执行“新用户”命令.在 打开的“新用 ...

  9. java基础知识回顾之javaIO类---BufferedInputStream和BufferedOutputStream

    MP3的复制过程: package com.lp.ecjtu; import java.io.BufferedInputStream; import java.io.BufferedOutputStr ...

  10. The Kernel Newbie Corner: Kernel Debugging with proc &quot;Sequence&quot; Files--Part 3

    转载:https://www.linux.com/learn/linux-career-center/44184-the-kernel-newbie-corner-kernel-debugging-w ...