zoj2016--Play on Words
来源:互联网 发布:windows vista premium 编辑:程序博客网 时间:2024/05/16 08:03
Description
Some of the secret doors contain a veryinteresting word puzzle. The team of archaeologists has to solve itto open that doors. Because there is no other way to open thedoors, the puzzle is very important for us.
There is a large number of magnetic plates on every door. Everyplate has one word written on it. The plates must be arranged intoa sequence in such a way that every word begins with the sameletter as the previous word ends. For example, the word "acm" canbe followed by the word "motorola". Your task is to write acomputer program that will read the list of words and determinewhether it is possible to arrange all of the plates in a sequence(according to the given rule) and consequently to open thedoor.
Input
The input consists of T test cases. The numberof them (T) is given on the first line of the input. Each test casebegins with a line containing a single integer number Nthatindicates the number of plates (1 <= N<= 100000). Then exactly Nlines follow, eachcontaining a single word. Each word contains at least two and atmost 1000 lowercase characters, that means only letters 'a' through'z' will appear in the word. The same word may appear several timesin the list.
Output
Your program has to determine whether it ispossible to arrange all the plates in a sequence such that thefirst letter of each word is equal to the last letter of theprevious word. All the plates from the list must be used, eachexactly once. The words mentioned several times must be used thatnumber of times.
If there exists such an ordering of plates, your program shouldprint the sentence "Ordering is possible.". Otherwise, output thesentence "The door cannot be opened.".
If there exists such an ordering of plates, your program shouldprint the sentence "Ordering is possible.". Otherwise, output thesentence "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.
////////////////////////////////////////////////////////////////
Ordering is possible.
The door cannot be opened.
////////////////////////////////////////////////////////////////
# include<stdio.h>
# include<string.h>
int set[27],head[27],tail[27],mark[27];
int find(int x)
{
if(set[x]!=x)
set[x]=find(set[x]);
return set[x];
}
void Combain(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx==fy)
return ;
else if(fx<fy)
set[fy]=fx;
else
set[fx]=fy;
}
int main()
{
int i,t,n,len,flag;
int a[27];
char s[1003];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<26;i++)
{
set[i]=i;
head[i]=0;
tail[i]=0;
mark[i]=0;
}
while(n--)
{
scanf("%s",s);
len=strlen(s)-1;
head[s[0]-'a']++;
tail[s[len]-'a']++;
mark[s[0]-'a']=1;
mark[s[len]-'a']=1;
Combain(s[0]-'a',s[len]-'a');
}
flag=0;
for(i=0;i<26;i++)
{
if(mark[i]&&set[i]==i)
flag++;
if(flag>=2)
break;
}
if(flag>=2)
printf("The door cannot be opened.\n");
else
{
flag=0;
for(i=0;i<26;i++)
{
if(mark[i]&&head[i]!=tail[i])
a[flag++]=i;
}
if(flag==0)
printf("Ordering is possible.\n");
else if(flag>2)
printf("The door cannot be opened.\n");
else if(flag==2)
{
if(head[a[0]]-tail[a[0]]==1&&tail[a[1]]-head[a[1]]==1||tail[a[0]]-head[a[0]]==1&&head[a[1]]-tail[a[1]]==1)
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
}
}
return 0;
}
0 0
- zoj2016--Play on Words
- poj 1386 Play on Words(欧拉回路…
- ZOJ2016 POJ1386 HDU1116 Play on Words,欧拉路径及欧拉回路
- UVA10129 POJ1386 HDU1116 ZOJ2016 Play on Words【欧拉回路+并查集】
- L - Play on Words
- HDOJ Play on Words
- poj1386(Play on Words)
- Play on Words
- poj1386 Play on Words
- 10129 - Play on Words
- 1116 Play on Words
- UVaOJ10129---Play on Words
- hdoj1116 Play on Words
- poj1116 Play on Words
- hdu play on words
- hdu1116 Play on Words
- uva10129 Play on Words
- Play on Words UVA10129
- poj2395--Out of Hay(最小生成树)
- poj1861--Network(最小生成树)
- HDU 1174 爆头
- 2011年08月13日
- poj1730--Perfect Pth Powers
- zoj2016--Play on Words
- 杭电1178--Heritage from father
- 谁能坚持超过50秒,我请他吃…
- poj3219--Binomial Coefficients
- 谁动了你的奶酪
- 杭电2553--N皇后问题(回溯)
- 杭电1258--确定比赛名次(拓扑排序…
- 杭电1715--大菲波数(大整数加法)
- 杭电1716--排列2(DFS)