码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces Round 290 div1 A

时间:2015-02-03 14:37:31      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

传送:http://codeforces.com/problemset/problem/510/C

 

拓扑排序 一个小trick,字典序不仅要求比较字母大小,还有长度~容易漏掉。。

 

#include <bits/stdc++.h>
using namespace std;

vector<int> v[101];
queue<int> q;
char str[101][101];
int in[101];
int len[101];
int main()
{
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        scanf("%s", str+i);
        len[i] = strlen(str[i]);
    }
    
    for(int i=1; i<n; i++)
    {for(int j=0; j<len[i]; j++)
        {
            if(j>=len[i+1])
            {
                printf("Impossible\n");
                return 0;
            }
            if(str[i][j] == str[i+1][j])
                continue;
            else
            {
                v[str[i][j]-a].push_back(str[i+1][j]-a);
                in[str[i+1][j]-a]++;break;
            }
        }
    }
    for(int i=0; i<26; i++)
            if(in[i] == 0)
                q.push(i);
    int temp = 0;
    string res;
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        temp ++;
        res = res + (char)(x+a);
        int size = v[x].size();
        for(int i=0; i<size; i++)
        {
            in[v[x][i]] --;
            if(!in[v[x][i]])
                q.push(v[x][i]);
        }
    }
    if(temp < 26)
        printf("Impossible\n");
    else
        cout<<res<<endl;
    return 0;
}

 

codeforces Round 290 div1 A

标签:

原文地址:http://www.cnblogs.com/ltwy/p/4269695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!