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

题目链接:  题目链接

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个字符串的大小是升序的。

比如:

abc

aad

如果它们两个是升序的的话,那么自定义的b肯定大于a。

然后输出任意一个符合条件的字母表。


题解:

其实题目比较简单,利用拓扑排序就可以实现该操作。

但是要注意字符串的比较是如何的。

遇到不相同的就返回其比较值,后面的无需比较

如果不能输出拓扑排序的序列,那么就无法实现。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL __int64
const int  MOD = 1e9 + 7;
const int N = 1e2 + 15;
const int INF = (1 << 30) - 1;
const int letter = 130;
using namespace std;
int n;
char str[N][N];
int mp[N][N];
bool dis[N][N];
int in[N], out[N];
bool vis[N];
queue<int>q;
vector<char>vs;
int value(char aim)
{
    if(aim == ' ') return 0;
    else return aim - 'a' + 1;
}
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        memset(vis, 0, sizeof(vis));
        memset(mp, 0, sizeof(mp));
        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
        memset(dis, 0, sizeof(dis));
        getchar();
        int max1 = -1, len;
        for(int i = 0; i < n; i++)
        {
            gets(str[i]);
            len = strlen(str[i]);
            if(len > max1) max1 = len;
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < max1; j++)
            {
                if(!(str[i][j] >= 'a' && str[i][j] <= 'z')) str[i][j] = ' ';
            }
            str[i][max1] = '\0';
        }
        int a, b;
        int t = 0;
        for(int j = 0; j < max1; j++)
        {
            for(int i = 0; i < n - 1; i++)
            {
                if(!dis[i][i + 1]) ///last
                {
                    a = value(str[i][j]);
                    b = value(str[i + 1][j]);
                    if(a != b)
                    {
                        dis[i][i + 1] = 1;
                        t++;
                       // in[b]++;
                        mp[a][b] = 1;
                     ///   printf("%d %d\n", a, b);
                    }
                    if(t == n - 1) break; ///bijiao n-1ci
                }
            }
            if(t == n - 1) break;
        }
        for(int i = 0;i <= 26;i++)
            for(int j = 0;j <= 26;j++)
        {
            if(mp[i][j]) in[j]++;
        }
        while(!q.empty()) q.pop();
        vs.clear();
        for(int i = 00; i <= 26; i++) if(in[i] == 0) q.push(i);
        if(in[0])
        {
            puts("Impossible");
            continue;
        }
        int flag = 1;
        while(!q.empty())
        {
            int x = q.front();
            q.pop();
            vs.push_back(x);
            for(int i = 0; i <= 26; i++)
            {
                if(mp[x][i])
                {
                    in[i]--;
                    if(in[i] == 0 && i != 0) q.push(i);
                    out[x]--;
                }
            }
        }

        for(int i = 1; i <= 26; i++)
        {
            if(in[i])
            {
                flag = 0;
                break;
            }
        }

        if(flag == 0) puts("Impossible");
        else
        {
            for(int i = 0; i < vs.size(); i++)
            {
                if(vs[i] != 0)
                {
                    printf("%c", vs[i] + 'a' - 1);
                    vis[vs[i]] = 1;
                }

            }
            for(int i = 1; i <= 26; i++)
            {
                if(!vis[i]) printf("%c", i + 'a' - 1);
            }
            printf("\n");
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值