POJ 2337 Catenyms 输出最小字母序欧拉路径 邻接表存储

来源:互联网 发布:gh0st 源码下载 编辑:程序博客网 时间:2024/05/29 04:03

Catenyms
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11566 Accepted: 3010

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.gophergopher.ratrat.tigeraloha.alohaarachnid.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

26alohaarachniddoggopherrattiger3oakmapleelm

Sample Output

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

Source

Waterloo local 2003.01.25

    题意:当一个单词的尾字母和另一个单词的首字母相同的时候,则这两个单词可以连接起来。给出一系列的单词,问最终能否连成一排,若可以连成一排,则输出其中字母序最小的那个。

    分析:欧拉路、欧拉回路系列问题。该题和POJ1386不同的是需要输出欧拉路径并且在存在多条的时候需要输出欧拉路径最小的。所以此时需要事先对字符串排个序。考虑到该题需要欧拉路径,所以可以使用邻接表的形式对边进行处理。数组实现邻接表的方法可参见:http://blog.csdn.net/qq_26071477/article/details/52599363。对于字符串可以将字符串看成一条边,首字母指向尾字母。该题在字母序最小方面的处理,可以在存储边的时候选出最小的起点或者终点,如果最终能生成欧拉回路的时候,就从这一点出发输出字符串。但如果最终生成的是欧拉路,则必须是从出度比入度大一的那一点出发输出欧拉路。该题是有向图判断欧拉路、欧拉回路的方法在此不赘述。最终dfs生成结果数组即可,注意由于递归的特性,在输出的时候需要注意从后向前输出。代码思路参见:http://www.cnblogs.com/kuangbin/p/3537544.html 。代码中有详细注释,在此给出:

// 判断单词是否连成一排的#include<stdio.h>#include<string.h>#include<string>#include<algorithm>#include<iostream>using namespace std;const int maxn=1005;//该题使用邻接表进行遍历struct edge{int to,next;//next表示该边的前一条边  以u为起点int id;int vis;//表示这条边有没有访问过} e[maxn];string s[maxn];int adj[27];//以i为起点的最后一条边的编号int in[27],out[27];//出度 入度数组int res[maxn];int cnt,num;int dfs(int s){for(int i=adj[s]; i!=-1; i=e[i].next)//遍历邻接表 从而遍历数组if(!e[i].vis)//保证每条边只走一次  生成欧拉路{e[i].vis=1;dfs(e[i].to);res[num++]=e[i].id;//结果数组中保存字符串数组的下标}}int  cmp(string sa,string sb)//大的边排在前面{return sa>sb;}int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);//初始化cnt=0,num=0;memset(adj,-1,sizeof(adj));memset(in,0,sizeof(in));memset(out,0,sizeof(out));//输入for(int i=0; i<n; i++)cin>>s[i];sort(s,s+n,cmp);//根据邻接表添加边的特性 每次后面的同一个起点的比都是添加在最前面int start=28;//字母序在0-25for(int i=0; i<n; i++)//添边{int u=s[i][0]-'a',v=s[i][s[i].length()-1]-'a';out[u]++;in[v]++;e[cnt].to=v;//该边指向ve[cnt].next=adj[u];//上一条边e[cnt].id=i;e[cnt].vis=0;adj[u]=cnt++;//越在前的边其字母序越大start=min(start,min(u,v));//记录最小的起点或者终点 以其作为最终生成的路径的起点 此处考虑到 是欧拉回路的情况}int c1=0,c2=0,c0=0,flag=1;for(int i=0; i<26; i++){if(in[i]-out[i]==1)//入度比出度大1的点的个数c2++;else if(in[i]-out[i]==-1)//出度比入度大1的点的个数{c1++;//此时是欧拉路  必须从出度比入度大1 的点出发start=i;//如果是欧拉路的话 则需要从出度比入度大一的点出发}else if(in[i]==out[i])c0++;else{flag=0;//如果存在 差值在0 +-1之外的情况则不可能构成欧拉路或者欧拉回路break;}}if(!flag||!((c1==0&&c2==0)||(c1==1&&c2==1))){printf("***\n");continue;}dfs(start);//在此使用dfs判连通 也可以使用并查集判连通//如果使用并查集判连通的话 则还需要dfs生成路径//使用dfs判连通的话可以直接根据返回的num的值判断图是否连通if(num!=n)//图不连通{printf("***\n");continue;}for(int i=num-1; i>=0; i--)//由于递归的特性  生成的小的值在数组最后{cout<<s[res[i]];if(i)printf(".");//输出需注意elseprintf("\n");}}}
    欧拉路系列算是小结,以后遇到再温习。

    特记下,以备后日回顾。

1 0
原创粉丝点击