Catenyms
poj2337:http://poj.org/problem?id=2337
题意:给定一些单词,如果一个单词的尾字母与另一个的首字母相同则可以连接。问是否可以每个单词用一次,将所有单词连接,可以则输出字典序最小的序列。 
题解:并查集+欧拉通路+贪心思维+dfs ,这一题我也是参考了别人的代码。 
  ps:vector的使用 ,内部堆栈的使用
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<stack>
using namespace std;
struct Node{
int v;
char ss[];//储存每个单词
bool vis;//标记该边是否被访问
Node(){ //初始化
v=;
vis=false;
}
bool operator<(Node a)const{//比较字符串,把字典序在前的放在前面
if(strcmp(ss,a.ss)<)return true;
else
return false;
}
};
vector<Node>map[];
int pa[];
void init(){//初始化
for(int i=;i<=;i++){
pa[i]=-;
map[i].clear(); }
}
int Find(int x){//查找
int s;
for(s=x;pa[s]>=;s=pa[s]);
while(s!=x){
int temp=pa[x];
pa[x]=s;
x=temp;
}
return s;
}
void Union(int R1,int R2){//合并
int r1=Find(R1);
int r2=Find(R2);
int temp=pa[r1]+pa[r2];
if(pa[r1]>pa[r2]){
pa[r1]=r2;
pa[r2]=temp;
}
else{
pa[r2]=r1;
pa[r1]=temp;
}
}
stack<char *> sta;//储存结果集
int in[],num;//入度
int out[];//出度
int used[];//标记出现过的字母
bool judge1(){//判断是否存在欧拉路径或者欧拉回路
num==-;int r1=;int r2=;
int in_num=,out_num=;
num=-;
for(int i= ; i<= ; ++i){
if(used[i]){
if(in[i]==out[i])continue;
else if(in[i]-out[i]==) in_num++;
else if(out[i]-in[i]==) out_num++,num=i;
else return false;
}
}
/*任意一点都可以开始*/
if(in_num==&&out_num==)
return true;
else if(in_num==&out_num==)
return true;
else return false;
}
bool judge2(){//判断是否连通
int first =-;
for(int i=;i<=;i++){
if(used[i]){
if(first==-)first=Find(i);
else if(first!=Find(i))return false;
}
}
return true;
}
void DFS(int key){//DFS寻找最小的路径
int size=map[key].size();
for(int i= ;i<size ; ++i){
int v=map[key][i].v;
if(!map[key][i].vis){
map[key][i].vis=; //标记已经被访问
DFS(v); //继续收索
sta.push(map[key][i].ss);//退出时把结果放入结果集
}
}
}
int main(){
int cas,n;char ss[];
scanf("%d",&cas);
while(cas--){
memset(in,,sizeof(in)),memset(out,,sizeof(out)),memset(used,,sizeof(used));
scanf("%d",&n);init();
while(!sta.empty())sta.pop();
for(int i=;i<=n;i++){
scanf("%s",ss);
int len=strlen(ss);
int u=ss[]-'a'+;int v=ss[len-]-'a'+;
out[u]++;in[v]++;used[u]=true;used[v]=true;
Node temp;
temp.v=v;strcpy(temp.ss,ss);
map[u].push_back(temp);
if(Find(u)!=Find(v))
Union(u,v);
}
bool can=judge2();
bool can1=judge1();
if(can&&can1){
for(int i=;i<=;i++){
if(used[i])
sort(map[i].begin(),map[i].end());//排序,是的结果集最小
}
if(num==-){
for(int i=;i<=;i++){
if(used[i]){//从最小的开始
DFS(i);
break;
}
}
}
else DFS(num);
char *str = sta.top();
sta.pop();
printf("%s",str);
while(!sta.empty()) {
str = sta.top();
sta.pop();
printf(".%s",str);
}
printf("\n");
}
else puts("***");
}
}
Catenyms的更多相关文章
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
		
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
 - POJ  2337  Catenyms  (欧拉回路)
		
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 2149 Descript ...
 - UVA 10441 - Catenyms(欧拉道路)
		
UVA 10441 - Catenyms 题目链接 题意:给定一些单词,求拼接起来,字典序最小的,注意这里的字典序为一个个单词比过去,并非一个个字母 思路:欧拉回路.利用并查集判联通,然后欧拉道路判定 ...
 - POJ2337 Catenyms(欧拉通路的求解)
		
Catenyms Time Limit: 1000MS Memory Limi ...
 - Catenyms (POJ2337) 字典序最小欧拉路
		
// 很久不写图论,连模板也不熟悉了0.0 // 这题是一个技巧性比较高的暴力DFS Catenyms 题目大意 定义catenym为首尾字母相同的单词组成的单词对, 例如: dog.gopher g ...
 - Day 4 -E - Catenyms POJ - 2337
		
A catenym is a pair of words separated by a period such that the last letter of the first word is th ...
 - Java实现Catenyms(并查集+dfs+欧拉回路)
		
Description A catenym is a pair of words separated by a period such that the last letter of the firs ...
 - POJ 2337 Catenyms(有向图的欧拉通路)
		
题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...
 - Catenyms POJ - 2337(单词+字典序输出路径)
		
题意: 就是给出几个单词 看能否组成欧拉回路或路径 当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点 把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...
 
随机推荐
- UML进行Linux内核调试
			
http://www.lenky.info/ http://blog.csdn.net/ztz0223/article/details/7874759 http://user-mode-linux.s ...
 - Optimized Pagination using MySQL---reference
			
Dealing with large data sets makes it necessary to pick out only the newest or the hottest elements ...
 - Java NIO——Selector机制源码分析---转
			
一直不明白pipe是如何唤醒selector的,所以又去看了jdk的源码(openjdk下载),整理了如下: 以Java nio自带demo : OperationServer.java Oper ...
 - [转] JavaScript中的属性:如何遍历属性
			
在JavaScript中,遍历一个对象的属性往往没有在其他语言中遍历一个哈希(有些语言称为字典)的键那么简单.这主要有两个方面的原因:一个是,JavaScript中的对象通常都处在某个原型链中,它会从 ...
 - 蓝牙代理报错:invalid handle error
			
错误症状: -(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCh ...
 - Python CMDB开发
			
Python CMDB开发 运维自动化路线: cmdb的开发需要包含三部分功能: 采集硬件数据 API 页面管理 执行流程:服务器的客户端采集硬件数据,然后将硬件信息发送到API,API负责将获取 ...
 - 很好用的Tab标签切换功能,延迟Tab切换。
			
一个网页,Tab标签的切换是常见的功能,但我发现很少有前端工程师在做该功能的时候,会为用户多想想,如果你觉得鼠标hover到标签上,然后切换到相应的内容,就那么简单的话,你将是一个不合格的前端工程师啊 ...
 - 关于git status
			
如果只在本地修改,还没有commit,那么用git status, 打印信息为: 如果我本地没有修改文件,就是:
 - C# 各种集合
			
大多数集合都在 System.Collections,System.Collections.Generic两个命名空间. 其中System.Collections.Generic专门用于泛型集合. ...
 - WPF Binding值转换器ValueConverter使用简介(一)
			
WPF.Silverlight及Windows Phone程序开发中往往需要将绑定的数据进行特定转换,比如DateTime类型的时间转换为yyyyMMdd的日期,再如有一个值是根据另外多组值的不同而异 ...