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.
PS: 其实就是将每个字符串首尾相连,看是否所有的字符串都能用到
The input consists of T test cases. The number of them (T) is given on the first line of the input. 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.
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.”.
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
学习大佬的代码 UVA10129 POJ1386 HDU1116 ZOJ2016 Play on Words【欧拉回路+并查集】
通过使用欧拉回路 + 并查集合判断
就是把连字符的游戏想成是一个图,首字符和结尾字符想成为一个定点,他们中间是边,就是看能不能组成一个欧拉回路,这样的话所有的都可以满足。
欧拉回路: 只要出度数等于入度数,或者入度数和出度数差1就是
并查集: 用于判断图是否联通
遇到的主要问题就是一直出现 Segmentation Fault
出现这个问题不是越界了就是栈溢出之类的。
后来发现是存放字符串的空间开小了…
(PS: 没注意看题目要求的字符串大小)
#include
#include
#include
using namespace std;
const int N = 26;
int f[N], cnt;
void UFInit(int n) {
for (int i = 0; i < n;i++) {
f[i] = i;
}
cnt = n;
}
int find(int a) {
return a == f[a] ? a : f[a] = find(f[a]);
}
void Union(int a, int b)
{
a = find(a);
b = find(b);
if (a != b) {
f[a] = b;
cnt--;
}
}
int degreeout[N], degreein[N];
bool degreeincheck()
{
int startcount = 0, endcount = 0;
for(int i=0; i 1)
return true;
} else if((degreeout[i] - degreein[i]) == -1) {
if(++endcount > 1)
return true;
} else
return true;
}
return false;
}
int main(int arg, char * args) {
int t = 0, l = 0;
scanf("%d", &t);
char str[100005];
int start = 0, end = 0;
while(t-- > 0) {
UFInit(N);
memset(degreeout, 0, sizeof(degreeout));
memset(degreein, 0, sizeof(degreein));
scanf("%d", &l);
for (int i = 0; i < l; ++i) {
scanf("%s", str);
start = str[0] - 'a';
end = str[strlen(str) - 1] - 'a';
degreeout[start]++;
degreein[end]++;
Union(start, end);
}
for (int i = 0; i < N; ++i) {
if (degreeout[i] || degreein[i]) {
;
} else {
cnt--;
}
}
bool hasPath = (cnt == 1);
if (hasPath) {
hasPath = ! degreeincheck();
}
if (hasPath) {
printf("Ordering is possible.\n");
} else {
printf("The door cannot be opened.\n");
}
}
return 0;
}