zoukankan      html  css  js  c++  java
  • 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');
    }
  • 相关阅读:
    redis:string字符串类型的操作
    redis键值操作
    什么是redis?redis有什么用途?
    centos7下安装配置redis
    windows下安装和配置redis
    mysql5.7.20完全卸载 win10
    selenium+PhantomJS小案例—爬豆瓣网所有电影代码python
    用单进程、多线程并发、多线程分别实现爬一个或多个网站的所有链接,用浏览器打开所有链接并保存截图 python
    PhantomJS、CasperJS安装配置图文详解
    python:什么是单例?一个简单的单例
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5077183.html
Copyright © 2011-2022 走看看