1. 程式人生 > >Play on Words UVA-10129

Play on Words UVA-10129

問題描述

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.

輸入


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 出現這個問題不是越界了就是棧溢位之類的。 發現是存放字串的空間開小了。。 改了一早晨,程式碼都塊一樣了。

程式碼如下

#include <stdio.h>
#include <iostream>
#include <string.h>

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<N; i++)
        if(degreeout[i] != degreein[i]) {
            if((degreeout[i] - degreein[i]) == 1) {
                if(++startcount > 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;
}