codeforces 510 C Fox And Names【拓扑排序】

题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序

先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度

然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入队列中,并将它指向的节点的入度减去1

另外如果len[i]<len[i-1],说明第i个字符串是i-1个字符串的前缀,但是它还排在后面,这样肯定不存在符合的字典序,直接输出“Impossible”

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include <cmath>
 5 #include<stack>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<algorithm>
11 using namespace std;
12
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=100005;
17
18 int e[maxn],first[maxn],next[maxn],ans[maxn],len[maxn],vis[maxn],in[maxn];
19 char s[105][105];
20 int ecnt;
21
22 void addedges(int u,int v){
23     e[ecnt]=v;
24     next[ecnt]=first[u];
25     first[u]=ecnt;
26     in[v]++;
27     ecnt++;
28 }
29
30 int main(){
31
32 //    freopen("in.txt","r",stdin);
33 //    freopen("out.txt","w",stdout);
34     int n;
35     cin>>n;
36
37     memset(first,-1,sizeof(first));
38     for(int i=1;i<=n;i++){
39         cin>>s[i];
40         len[i]=strlen(s[i]);
41     }
42
43
44     int j;
45     ecnt=0;
46     for(int i=2;i<=n;i++){
47         for(j=0;j<min(len[i],len[i-1]);j++){
48             if(s[i-1][j]!=s[i][j]){
49                 addedges(s[i-1][j]-‘a‘,s[i][j]-‘a‘);
50                 break;
51             }
52         }
53         if(len[i]<len[i-1]&&j==len[i]){
54             printf("Impossible\n");
55             return 0;
56         }
57     }
58
59
60     memset(vis,0,sizeof(vis));
61
62     for(int i=1;i<=26;i++){
63         int jj=26;
64         int j;
65         for( j=0;j<26;j++){
66             if(!vis[j]&&in[j]==0){
67                 vis[j]=1;
68                 ans[i]=j;
69                 for(int k=first[j];k!=-1;k=next[k]){
70                     in[e[k]]--;
71                 }
72                 jj=j;
73                 break;
74             }
75         }
76         if(jj==26){//jj的值没有发生改变,说明这 一点找不到符合的拓扑序
77             printf("Impossible\n");
78             return 0;
79         }
80     }
81
82     for(int i=1;i<=26;i++)
83     printf("%c",ans[i]+‘a‘);
84     printf("\n");
85     return 0;
86 }

时间: 2024-11-05 19:36:44

codeforces 510 C Fox And Names【拓扑排序】的相关文章

(CodeForces 510C) Fox And Names 拓扑排序

题目链接:http://codeforces.com/problemset/problem/510/C 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.

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 510C Fox And Names 拓扑排序

传送门:cf 510D 给定n个字符串,问能否存在这样的字母表,使得字符串的排序满足字典序.即依据新的字母表,排序满足字典序大小. 假设满足字典序,则我们可以依据已有的字符串得出各字母之间的大小关系,然后通过拓扑排序来判断是否存在可行解,输出任意解,因此只需要判断是否存在解即可. /****************************************************** * File Name: a.cpp * Author: kojimai * Create Time: 2

[CF #290-C] Fox And Names (拓扑排序)

题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来,将每个名字的第x个字母从上到下连接建图.然后求拓扑排序. 之所以要拓扑排序,因为要判断在x-1里面有a-->b  在x中有b-->a,这样就形成了一个环.这样一来,就不能够构造字母表了. [经验教训]:在递归建图的函数中开了两个数组,用来记录字母第一次出现和最后一次出现的位置..结果就RE在12上

CF Fox And Names (拓扑排序)

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

codeforces 510C Fox And Names 拓扑

题意:n个姓名,按照某种"字典序". 问如果存在这样的字典序,输出字典序'a'到'z'26个字母的顺序. 思路:拓扑排序.对于str[i]和str[i+1]如果在位置k出现不同,那么x=str[i][k]-'a'+1,y=str[i+1][k]-'a'+1,从x->y连一条边,y的入度in[y]++. 然后拓扑排序,如果形成环,就说明不行,不然依次输出对应字符.(ps:len1为str[i]的长度,len2为str[i+1]的长度,如果len1>len2且前len2个均相同

CodeForces 510 B. Fox And Two Dots(DFS 啊)

题目链接:http://codeforces.com/problemset/problem/510/B Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n?×?m cells, like this: Each cell contains a dot that has some color. We will use diff

CodeForces 510 A. Fox And Snake(模拟啊 )

题目链接:http://codeforces.com/problemset/problem/510/A Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead. A snake is a pattern on a n by m

Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/29/C Description One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city «A» to city «B