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

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个字符串,它们按照某个字典序从小到大排列,问这个字典序是否存在,存在就输出任意一个满足条件的字典序,否则输出“Impossible”。

裸的topsort,结果在终判时挂了,就因为没有特判,杯具。。。。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#define maxn 111
using namespace std;

int indegree[maxn],path[maxn];
int n,m;
char w[105][105];
queue<int>Q;
vector<int>edges[10000];

void topsort()
{
    memset(path,0,sizeof(path));
    while (!Q.empty())
        Q.pop();
    for (int i=0;i<26;i++)
        if (!indegree[i])
            Q.push(i);
    int num=0;
    while (!Q.empty())
    {
        int now=Q.front();
        Q.pop();
        path[num++]=now;
        for (int i=0;i<edges[now].size();i++)
        {
            --indegree[ edges[now][i]];
            if (!indegree[ edges[now][i] ])
            {
                Q.push(edges[now][i]);
            }
        }
    }
    if (num<26)
    {
        printf("Impossible\n");
        return ;
    }
    for (int i=0;i<num;i++)
        printf("%c",path[i]+'a');
    printf("\n");
}

int main()
{
    while (~scanf("%d",&n))
    {
        memset(indegree,0,sizeof(indegree));
        for (int i=1;i<=n;i++)
            edges[i].clear();
        int u,v;
        for (int i=0;i<n;i++)
            scanf("%s",w[i]);
        int flag=0;
        for (int i=1;i<n;i++)
        {
            int a=i-1,b=i;
            int la=strlen(w[a]);
            int lb=strlen(w[b]);
            int aa=0,bb=0;
            while (aa<la&&bb<lb&&w[a][aa]==w[b][bb])
            {
                aa++;
                bb++;
            }
            if (bb==lb&&aa<la) {flag=1; break;} //掉了这一句,结果挂了。。。
            if (aa==la&&bb<=lb) continue;
            indegree[w[b][bb]-'a']++;
            edges[w[a][aa]-'a'].push_back(w[b][bb]-'a');
        }
        if (flag)
        {
            printf("Impossible\n");
            continue;
        }
        topsort();
    }
    return 0;
}
时间: 2024-10-11 12:03:07

C. Fox And Names Codeforces Round #290 (Div. 2)的相关文章

Codeforces Round #290 (Div. 2) b

/** * @brief Codeforces Round #290 (Div. 2) b * @file a.cpp * @author mianma * @created 2015/02/04 15:17 * @edited 2015/02/04 15:17 * @type brute * @note */ #include <fstream> #include <iostream> #include <string> #include <cstring>

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: t

Codeforces Round #290 (Div. 2) 解题报告 A.B.C.D.

A - Fox And Snake 模拟. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h> using na

Codeforces Round #290 (Div. 2) 拓扑排序

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: t

Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring" int r,c; char map[55][55]; int vis[55][55]; int mark; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int judge(int x,int y) { if(x<0||x>=r||y<0||y>

B. Fox And Two Dots Codeforces Round #290 (Div. 2)

B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n?×?m ce

Codeforces Round #290 (Div. 2) B (dfs)

题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相同且已搜过,则存在满足题意的环 代码: 1 #include <bits/stdc++.h> 2 #define MAXN 60 3 using namespace std; 4 5 int mp[MAXN][MAXN], vis[MAXN][MAXN], m, n; 6 int dir[4][2

Codeforces Round #290 (Div. 2)

A题: 简单的模拟. 贴样例就知道了. input 3 3 output ###..#### input 3 4 output ####...##### input 5 3 output ###..#####..### input 9 9 output #########........###########........#########........###########........######### 1 #include<cstdio> 2 int main() 3 { 4 in

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/