Skip to main content
  1. Posts/

codeforces 510 C. Fox And Names

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://codeforces.com/contest/510/problem/C

题意:给定n个字符串。问是否存在一种字母顺序,使得这n个字符串的顺序满足字典序(自定义的)。如果有多种顺序,输出字典序(标准的)最小的。

思路:将字符串的关系处理成边的关系。每次对于第i个和第i+1个字符串,从前往后扫,直到不相等的那一位,设为k,然后连边,指向i+1。表明第i个字符串的第k位大于第i+1个字符串的第k位。如果没有不想等的。说明其中一个是另一个的字串。如果前者是后者的字串,那么不影响。如果后者是前者的字串,则不存在满足条件的字典序。然后做拓扑排序。由于有多种输出字典序(标准的)最小的方案。所以存点的时候用优先队列存。

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2015年12月06日 星期日 15时16分50秒
  4File Name :code/cf/problem/510C.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25
 26
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=105;
 34int n;
 35char st[N][N];
 36int p[30];
 37int offset[30];
 38bool conc[30][30];
 39int in[N];
 40int ans[N];
 41struct node
 42{
 43    int x;
 44    node(int xx)
 45    {
 46	x=xx;
 47    }
 48    bool operator <(const node a)const{
 49	return x>a.x;
 50    }
 51};
 52bool getconc( int x,int y)
 53{
 54    int lx = strlen(st[x]);
 55    int ly = strlen(st[y]);
 56    int len = min(lx,ly);
 57    for ( int i =  0; i < len ; i++)
 58    {
 59	if (st[x][i]!=st[y][i])
 60	{
 61	    conc[st[x][i]-'a'][st[y][i]-'a'] = true;
 62	    return true;
 63	}
 64    }
 65  //  if (lx>ly) return false;
 66    return true;
 67}
 68bool pre()
 69{
 70    ms(conc,false);
 71    ms(in,0);
 72    scanf("%d",&n);
 73    for ( int i = 0 ;i  < n ; i++) scanf("%s",st[i]);
 74
 75    bool ok = true;
 76    for ( int i = 0 ; i < n-1 ; i++)
 77    {
 78	ok = getconc(i,i+1);
 79	if (!ok) break;
 80    }
 81    if (ok) return true;
 82    return false;
 83}
 84
 85bool topo()
 86{
 87    priority_queue<node>q;
 88    for ( int i = 0 ; i < 26 ; i++)
 89	if (in[i]==0)  q.push(i);
 90
 91    int cnt = 0 ;
 92
 93    while (!q.empty())
 94    {
 95	node v = q.top();q.pop();
 96	cnt++;
 97	ans[cnt]=v.x;
 98	for ( int i = 0 ; i < 26 ; i++)
 99	{
100	    if (!conc[v.x][i]) continue;
101	    in[i]--;
102	    if (in[i]==0) q.push(i);
103	}
104    }
105    if (cnt<26) return false;
106    return true;
107}
108int main()
109{
110	#ifndef  ONLINE_JUDGE
111	freopen("code/in.txt","r",stdin);
112  #endif
113	if (pre())
114	{
115	    for ( int i = 0 ; i < 26 ; i++)
116		for ( int j = 0 ; j < 26 ; j++)
117		    if (conc[i][j]) in[j]++;
118	    if (topo())
119	    {
120		for (int i = 1 ; i <= 26 ; i++)
121		    printf("%c",char(ans[i]+'a'));
122		puts("");
123	    }
124	    else
125	    {
126		puts("Impossible");
127	    }
128	}
129	else
130	{
131	    puts("Impossible");
132	}
133
134
135  #ifndef ONLINE_JUDGE
136  fclose(stdin);
137  #endif
138    return 0;
139}

Related

hdu 2094 产生冠军

·1 min
题意:给定n组u关系。每组表示a战胜b。。问根据这些关系能否确定冠军。 思路:如果a战胜b就从a连一条指向b的边。那么能确定冠军的条件就变成了,有且只有一个入度为0的点。翻译过来就是,有一个人没有被任何人战胜过。且,这样的人只有一个。一开始想用map来搞。。但是比较麻烦。。其实用set比较好。。开两个set,一个存所有的人,一个存输过的人。出度为0的点只有一个等价为,有且只有一个人没有输过。也就是两个set的元素差个数为1.

hdoj 1285 确定比赛名次

http://acm.hdu.edu.cn/showproblem.php?pid=1285 题意: 有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。

codeforces 129 B. Students and Shoelaces

·2 mins
http://codeforces.com/contest/129/problem/B 题意:n个点。m条边。每一次会将图中度为1的点加入到等待队列中。然后一起删掉,记为一次操作。当删掉一个点的时候,与它相连的边也全部删掉。问一共做进行多少次操作。使得图中不再有度为1的点。 思路:重点是用开一个数组deg[i]记录点i的度。这样比用.size()高明太多。。因为我们并不需要知道具体删了哪条边。我们只要知道与点i相连的点的边数因为点i被删除而减少了1.

cf 596 B. Wilbur and Array

·1 min
http://codeforces.com/problemset/problem/596/B 题意:初始序列全为0,问经过多少次变换,能变成序列b。一次变换是指,选定一个i,从i一直到最后每个元素都增加1,或者每个元素都减少1. 思路:很容易发现。后面的变换补影响前面的变换。每一个数字可以唯一由之前的增加和减少次数决定。所以我们用两个变量,记录之前做的增加和减少变换的次数。然后扫一遍即可。

codeforces 600 C. Make Palindrome

·2 mins
http://codeforces.com/problemset/problem/600/C 题意:给定一个字符串。要求用最少的变换得到一个回文串。且在变换次数相同时要字典序最小的。输出变换后的字符串。 思路:对不能构成会文串有影响的是出现奇数次的字母。所以我们先统计每个字母出现的次数。然后按照出现奇数次的字母的个数分奇偶分别搞。偶数的话直接把后面一半变成前面一半。奇数的话,也是这样。输出的时候按照字母从a扫到z,如果有就输出一半。然后再倒着扫一遍。 输出另一半。这样可以保证是字典序最小。需要注意的是奇数的时候的输出情况。不要忘记中间那个字母。