zoukankan      html  css  js  c++  java
  • Codeforces 510C

    CodeForces 510C

    题意:给出n个名字,问是否存在一个字典序(这个字典序是任意的)使得这些名字是按这个字典序输出的,如果存在输出任意一个可行的字典序

    思路:拓扑排序,关键是建图,第i个和i+1个建一条由i指向i+1的单向边,建图是a-z26个字母间建图,每2个相邻的单词之间排序的依据是第一个不相同的字母,由这个不相同的字母之间建边,如果没有不相同的则不建边,这里有一个坑,就是 aaa aaaa这种数据,要特判

    AC代码:

    #include "iostream"
    #include "string.h"
    #include "stack"
    #include "queue"
    #include "string"
    #include "vector"
    #include "set"
    #include "map"
    #include "algorithm"
    #include "stdio.h"
    #include "math.h"
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ll long long
    #define endl ("
    ")
    #define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
    #define mem(a,x) memset(a,x,sizeof(a))
    #define mp(x,y) make_pair(x,y)
    #define pb(x) push_back(x)
    #define ft (frist)
    #define sd (second)
    #define lrt (rt<<1)
    #define rrt (rt<<1|1)
    using namespace std;
    const long long INF = 1e18+1LL;
    const int inf = 1e9+1e8;
    const int N=1e5+100;
    const ll mod=1e9+7;
    
    struct AN{
        int id,num;
        bool friend operator< (AN a, AN b){
            return a.num<b.num;
        }
    }ans[160];
    char s[155][155];
    int n,head[155],nex[305],to[305],tot=1,in[300],cnt;
    bool vis[155];
    void add(int u, int v){
        to[tot]=v;
        nex[tot]=head[u];
        head[u]=tot++;
    }
    
    void topu(int u){
        queue<int> Q;
        while(!Q.empty()) Q.pop();
        Q.push(u);
        vis[u]=1;
        while(!Q.empty()){
            int now=Q.front(); Q.pop();
            ans[now].num=++cnt;
            for(int i=head[now]; i!=-1; i=nex[i]){
                int v=to[i]; in[v]--;
                if(in[v]==0 && vis[v]==0){
                    Q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    int main(){
        ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
        cin>>n;
        mem(head,-1);
        for(int _=1; _<=n; ++_){
            cin>>s[_];
        }
        for(int i=1; i<n; ++i){
            int k=0;
            int l1=strlen(s[i]), l2=strlen(s[i+1]);
            while(s[i][k]==s[i+1][k] && k<=l1 && k<=l2){
                ++k;
            }
            if(k>=l1 || k>=l2){
                if(l2<l1){
                    cout<<"Impossible
    ";
                    return 0;
                }
                continue;
            }
            add(s[i][k]-'a'+1, s[i+1][k]-'a'+1);
            in[s[i+1][k]-'a'+1]++;
        }
        for(int i=1; i<=26; ++i){ //cout<<in[i]<<" ";
            if(in[i]==0 && vis[i]==0) topu(i);//bug(ans[i].num)
        }
        for(int i=1; i<=26; ++i){
            if(ans[i].num==0){
                cout<<"Impossible
    ";
                return 0;
            }
        }
        for(int i=1; i<=26; ++i) ans[i].id=i;
        sort(ans+1,ans+1+26);
        for(int i=1; i<=26; ++i) cout<<char(ans[i].id+'a'-1);
        return 0;
    }
  • 相关阅读:
    D3 data
    cubism.js
    git
    Render函数
    Vue 响应式原理
    JSSDK使用步骤
    用js获取access_token
    微信公众平台appid和appsecret在哪
    组件
    表单控件绑定
  • 原文地址:https://www.cnblogs.com/max88888888/p/7306058.html
Copyright © 2011-2022 走看看