• 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 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 si and 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 Input

    3

    rivest

    shamir

    adleman

    Sample Output

    bcdefghijklmnopqrsatuvwxyz

    Hint

    题意

    给你n个串,然后让你输出一个字符串,使得根据这个字符串的先后顺序排序的n个串

    和给你的顺序是一样的

    题解:

    每两个串相比较,只需要一对字符不一样

    记录一下是哪一对,然后再dfs一波就好了

    注意坑点:

    有可能两个串只有长度不一样

    形成环

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    string s[120];
    vector<int> G[30];
    int flag = 0;
    int ran[40];
    int vis[120],used[120];
    int tot = 0;
    void solve(int x)
    {
        for(int i=0;i<s[x].size()&&i<s[x+1].size();i++)
        {
            if(s[x][i]!=s[x+1][i])
            {
                G[s[x+1][i]-'a'].push_back(s[x][i]-'a');
                return;
            }
        }
        if(s[x+1].size()<s[x].size())
        {
            puts("Impossible");
            exit(0);
        }
    }
    void dfs(int x)
    {
        vis[x]=used[x]=1;
        for(int i=0;i<G[x].size();i++)
        {
            if(used[G[x][i]])
            {
                puts("Impossible");
                exit(0);
            }
            if(!vis[G[x][i]])
                dfs(G[x][i]);
        }
        used[x]=0;
        ran[tot++]=x;
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            cin>>s[i];
        for(int i=0;i<n-1;i++)
            solve(i);
        for(int i=0;i<26;i++)
        {
            memset(used,0,sizeof(used));
            if(!vis[i])
                dfs(i);
        }
        for(int i=0;i<26;i++)
            printf("%c",ran[i]+'a');
    }
  • 相关阅读:
    数据挖掘实践(9):基础理论(九)数学基础(九)概率(五)⻉叶斯公式
    数据挖掘实践(8):基础理论(八)数学基础(八)概率(四)参数估计(二)
    数据挖掘实践(7):基础理论(七)数学基础(七)概率(三)参数估计(一)
    数据挖掘实践(6):基础理论(六)数学基础(六)概率(二)随机变量
    Linux上快速入门英特尔Optane DC Persistent Memory Module的配置与使用
    ovs 流表
    Cannot get thread event message: debugger service failed
    gdb ovs
    rror while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
    glibc
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5077183.html
Copyright © 2020-2023  润新知