codeforces 510c 拓扑排序

题目:

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

Examples
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个字符串,让你确定一个a-z的字典序(重新规定先后关系)使得这n个串是按照这个字典序给出的。


分析:

给出一个序列 -> 拓扑排序 

字符串前后两个进行比较来建图,不需要两两比较。找到第i个和第i+1个串第一个不相同的字母,前者向后者连一条边。若第i+1个串是第i个串的子串,这两个串肯定不符合字典序


拓扑排序思想:

对于一个DAG(有向无环图),拓扑排序的结果是将所有节点排成一个线性序列。

算法:

(1)将有向图中入度为0的节点放到序列的最前面(同时入队),这些节点之间没有顺序.  
(2)从图中删除这些顶点连出的所有边,及所连向的节点入度减一.  
(3)对于(2)得到的新图,重复执行(1)  (2),直至队列为空.



代码:

#include<algorithm>
#include<string>
#include<iostream>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 200;

vector<int> g[26];
string now,pre;
int in[26],ans[26];
int tot=0;

void toposort() {
    queue<int> que;
    for(int i=0; i<26; i++)  if(!in[i]) que.push(i);
    while(!que.empty()) {
        int x = que.front();
        ans[tot++] = x;
        que.pop();
        for(int i=0; i<g[x].size(); i++) {
        	in[g[x][i]]--;
            if(!in[g[x][i]])  que.push(g[x][i]);
        }
    }
    if(tot != 26) cout << "Impossible" << endl;
    else {
        for(int i=0; i<26; i++) {
            cout <<char('a'+ ans[i]);
        }
        cout << endl;
    }
}

int main(){
    int n;
    cin >> n;
    for(int i=0; i<n; i++) {
        cin >> now;
        int len = min(now.length(),pre.length());
        bool ok = false;
        for(int j=0; j<len; j++) {
            if(now[j] != pre[j]) {
                ok = true;
                g[pre[j]-'a'].push_back(now[j]-'a');
                in[now[j]-'a']++;
                break;
            }
        }
        if(!ok&&now.length() < pre.length()) {
            cout << "Impossible" << endl;
            return 0;
        }
        pre=now;
    }
    toposort();
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值