1. 程式人生 > >POJ2337 Catenyms(尤拉路徑)

POJ2337 Catenyms(尤拉路徑)

Description

A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For example, the following are catenyms: 

dog.gopher

gopher.rat

rat.tiger

aloha.aloha

arachnid.dog


A compound catenym is a sequence of three or more words separated by periods such that each adjacent pair of words forms a catenym. For example, 

aloha.aloha.arachnid.dog.gopher.rat.tiger 

Given a dictionary of lower case words, you are to find a compound catenym that contains each of the words exactly once.

Input

The first line of standard input contains t, the number of test cases. Each test case begins with 3 <= n <= 1000 - the number of words in the dictionary. n distinct dictionary words follow; each word is a string of between 1 and 20 lowercase letters on a line by itself.

Output

For each test case, output a line giving the lexicographically least compound catenym that contains each dictionary word exactly once. Output "***" if there is no solution.

Sample Input

2
6
aloha
arachnid
dog
gopher
rat
tiger
3
oak
maple
elm

Sample Output

aloha.arachnid.dog.gopher.rat.tiger
***

Source

Waterloo local 2003.01.25

 

思路:這道題目和我之前寫過的 P1341 無序字母對(歐拉回路)很相似,基本上是一個套路,不過這個題目比後者複雜一些,還得求是否聯通,建議先去看後面一個題目,參悟完了再來看這個題目,首先我們可以看出來這道是讓我們求尤拉路徑的,那麼我們先來建立圖,字串的起點到終點又一條有向的路徑,然後前向星建立圖,在建立圖前需要先排序,建立圖的時候要從後往前插入,因為前向星是從後往前讀取的,所以我們反正插入就可以得到字典序列的輸出,然後就是尤拉路徑的判斷了,這個題目和尤拉路徑有區別的地方在於所有的字串只輸出一次,而尤拉路徑是所有的邊都要輸出一次,如果咱們用尤拉路徑的方法去做可能會產生輸出重複字串的錯誤,所以這裡我們用一個flag來記錄改字串是記錄過,接下來看程式碼

 

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstdio>
using namespace std;
struct node{
    int to, index;
    bool flag;
    int next;
}edge[2010];

int head[30], cnt = 1, index, ans[2010], n, m, in[30], out[30];

void add(int u,int v,int index)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    edge[cnt].index = index;
    edge[cnt].flag = false;
    head[u] = cnt++;
}

void init() { //初始化
    fill(head, head + 30, 0);
    fill(in, in + 30, 0);
    fill(out, out + 30, 0);
    cnt = 1;
    index = 0;
}

void dfs(int u) {
    for (int i = head[u]; i != 0; i = edge[i].next) {
        int v = edge[i].to;
        if (!edge[i].flag) {
            edge[i].flag = true;
            dfs(v);
            ans[index++] = edge[i].index;
        }
    }
}

int main() {
    cin >> n;
    while (n--) {
        scanf("%d",&m);
        vector<string> v;
        string s;
        for(int i = 0;i < m;i++) {
            cin >> s;
            v.push_back(s);
        }
        sort(v.begin(), v.end());//排序
        init();
        int st = 1000;
        for(int i = m - 1; i >= 0; i--)//重點,一定要逆著插入
        {
            int u = v[i][0] - 'a';
            int vv = v[i][v[i].length() - 1] - 'a';
            add(u,vv,i);
            out[u]++;
            in[vv]++;
            st = min(u, min(vv, st)); //找最小的點
        }
        int cc1 = 0, cc2 = 0;
        for(int i = 0;i < 26;i++)
        {
            if(out[i] - in[i] == 1) // 如果出度比入讀大1,那麼改點為起點
            {
                cc1++;
                st = i; 
            }
            else if(out[i] - in[i] == -1)
                cc2++;
            else if(out[i] - in[i] != 0)
                cc1 = 3;
        }
        if(! ( (cc1 == 0 && cc2 == 0) || (cc1 == 1 && cc2 == 1) )) //如果所有頂點初度等於入讀或者只有起點和終點不符合這條規矩,那麼進行dfs,否則輸出***
        {
            cout << "***" << endl;
            continue;
        }
        dfs(st);
        if(index != m) // 判斷圖的聯通,也就是數組裡面的節點個數是不是等於輸入的個數
        {
             cout << "***" << endl;
            continue;
        }
        for(int i = index-1; i >= 0;i--)
        {
            cout<<v[ans[i]];
            if(i > 0) cout << ".";
        }
        cout << endl;
    }
    return 0;
}


解釋完畢