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

2016暑假集训训练2 N题 Play on Words

时间:2016-07-22 21:20:10      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

水题不解释。。。。

 

# include <stdio.h>
# include <string.h>
int dee[26], set[26], node[26], n;
char s[1002];

int find(int x)
{
	int s, temp;
	for (s=x; set[s]>=0; s=set[s])
		;
	while (s!=x) {
		temp = set[x];
		set[x] = s;
		x = temp;
	}
	return s;
}

void union_set(int x, int y)
{
	int a, b, temp;
	a = find(x);
	b = find(y);
	temp = set[a] + set[b];
	if (set[a] > set[b]) {
		set[a] = b;
		set[b] = temp;
	}
	else {
		set[b] = a;
		set[a] = temp;
	}
}

int main (void)
{
	int t, i, j, u, v, len, flag, cnt1, cnt2;
	scanf("%d",&t);
	while (t--) {
		flag = 0;
		memset(set,-1,sizeof(set));
		memset(dee,0,sizeof(dee));
		memset(node,0,sizeof(node));
		scanf("%d",&n);
		while (n--) {
			scanf("%s",s);
			len = strlen(s);
			u = s[0] - ‘a‘;
			v = s[len-1] - ‘a‘;
			dee[u]++;
			dee[v]--;
			node[u] = node[v] = 1;
			if (find(u) != find(v))
				union_set(u,v);
		}
		for (i=0, j=0; i<26; i++) 
			if (node[i] == 1)
				node[j++] = i;
		for (i=0; i<j-1; i++) 
			if (find(node[i])^find(node[i+1])) {
				flag = 1;
				break;
			}	
		if (flag) {
			puts("The door cannot be opened.");
			continue;
		}
		for (i=0, cnt1=0, cnt2=0; i<j; i++) {
			if (dee[node[i]] == 1)
				cnt1++;
			else if (dee[node[i]] == -1)
				cnt2++;
			else if (dee[node[i]] != 0) {
				flag = 1;
				break;
			}
		}
		if (flag ||(cnt1!=1 && cnt1)|| (cnt2!=1 && cnt2))
			puts("The door cannot be opened.");
		else 
			puts("Ordering is possible.");
	}
	return 0;
}

  

2016暑假集训训练2 N题 Play on Words

标签:

原文地址:http://www.cnblogs.com/lishiyao/p/5697078.html

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