亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Play on Words[HDU1116]

系統 1953 0

Play on Words

Time Limit: 10000/5000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4094????Accepted Submission(s): 1328

?

Problem Description
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.

?

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
?

?

Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.
?

?

Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".
?

?

Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
?

?

Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
?

?

Source
Central Europe 1999
?

?

Recommend
Eddy

Take letters 'a'..'z' as the nodes in graph.A word start with charcateristic c1 and end by characteristic c2 forms an edge in the graph.Then the problem become if there exist an Euler circuit.
So how can we judge whether a graph exists an Euler circuit or not ? First the graph should be a connected graph.We can determine this by breadth-first search.Then we have to check the in-degree and out-degree.Only two or zero if them can be different.If there are two difference,one of the two its in-degree minus its out-degree should be one while the other should be minus one.

?

      #include<stdio.h>

#include<string.h>

#include<queue>

#include<iostream>

using namespace std;

int dr[26],dc[26],N,f[26];

bool e[26],g[26][26];

bool bfs()

{

	int i;

	memset(f,0,sizeof(f));

	queue<int> q;

	while (!q.empty()) q.pop();

	for (i=0;i<26;i++)

		if (e[i])

		{

			f[i]=true;

			q.push(i);

			break;

		}

	while (!q.empty())

	{

		int x=q.front();

		q.pop();

		for (i=0;i<26;i++)

			if (g[x][i])

			{

				if (!f[i])

				{

					f[i]=true;

					q.push(i);

				}

			}

	}

	for (i=0;i<26;i++)

		if ((e[i]) && (!f[i])) return false;

	return true;

}

int main()

{

	int T,i;

	scanf("%d",&T);

	while (T--)

	{

		scanf("%d",&N);

		char s[1024];

		memset(g,0,sizeof(g));

		memset(e,0,sizeof(e));

		memset(dr,0,sizeof(dr));

		memset(dc,0,sizeof(dc));

		for (i=1;i<=N;i++)

		{

			scanf("%s",s);

			int u=s[0]-'a',v=s[strlen(s)-1]-'a';

			g[u][v]=true;

			g[v][u]=true;

			e[u]=true;

			e[v]=true;

			dc[u]++;

			dr[v]++;

		}

		bool flag=bfs();

		int cnt0=0,cnt1=0;

		for (i=0;i<26;i++)

		{

			if (dr[i]-dc[i]>1 || dr[i]-dc[i]<-1) flag=false;

			if (dr[i]-dc[i]==1) cnt0++;

			if (dr[i]-dc[i]==-1) cnt1++;

		}

		if (!((cnt0==0 && cnt1==0)||(cnt0==1 && cnt1==1))) flag=false;

		if (flag) printf("Ordering is possible.\n");

		else printf("The door cannot be opened.\n");

	}

	return 0;

}


    

?

?

?

Play on Words[HDU1116]


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产在线精品观看一区 | 99re视频精品全部免费 | 欧美13一14周岁a在线播放 | 91尤物视频在线观看 | 欧美成人免费夜夜黄啪啪 | 日韩视频在线观看一区 | 欧美久久综合网 | 色婷五月 | 四虎永久免费影院在线 | 成年人一级毛片 | 人人夜| 99热这里只有精品免费国产 | 一级毛片私人影院 | 黑丝毛片 | 久久精品资源 | 国产一区二区三区四 | 米奇影视7777| 伊人天天躁夜夜躁狠狠 | 夜夜夜夜猛噜噜噜噜噜 | 中文字幕日韩亚洲 | 日本久久黄色 | 欧美成人性色xxxx视频 | 国产福利91精品一区二区三区 | 99久久国产综合精品五月天 | 国产精品国产高清国产专区 | 免费观看一级成人毛片软件 | 色婷婷免费视频 | 狠狠色噜狠狠狠狠色综合久 | 久久精品二区 | 青青青青久久精品国产一百度 | 天堂成人一区二区三区 | 天堂一区二区三区在线观看 | 四虎影视精品 | 青青热久免费精品视频精品 | 自拍亚洲午夜伦li片影院 | 97高清| 久久夜夜操 | 亚洲欧美精品综合中文字幕 | 婷婷开心中文字幕 | 精品无人区乱码一区二区三区手机 | 亚洲精品久久久久中文字幕一区 |