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的更多相关文章

  1. POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8756   Accepted: 2306 Descript ...

  2. POJ 2337 Catenyms (欧拉回路)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8173   Accepted: 2149 Descript ...

  3. UVA 10441 - Catenyms(欧拉道路)

    UVA 10441 - Catenyms 题目链接 题意:给定一些单词,求拼接起来,字典序最小的,注意这里的字典序为一个个单词比过去,并非一个个字母 思路:欧拉回路.利用并查集判联通,然后欧拉道路判定 ...

  4. POJ2337 Catenyms(欧拉通路的求解)

                                                               Catenyms Time Limit: 1000MS   Memory Limi ...

  5. Catenyms (POJ2337) 字典序最小欧拉路

    // 很久不写图论,连模板也不熟悉了0.0 // 这题是一个技巧性比较高的暴力DFS Catenyms 题目大意 定义catenym为首尾字母相同的单词组成的单词对, 例如: dog.gopher g ...

  6. 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 ...

  7. Java实现Catenyms(并查集+dfs+欧拉回路)

    Description A catenym is a pair of words separated by a period such that the last letter of the firs ...

  8. POJ 2337 Catenyms(有向图的欧拉通路)

    题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...

  9. Catenyms POJ - 2337(单词+字典序输出路径)

    题意: 就是给出几个单词 看能否组成欧拉回路或路径  当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点   把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...

随机推荐

  1. 系统调用和中断处理的异同(以Linux MIPS为例)

    在Linux下写一个驱动时候遇到的读操作性能问题,让我想一窥系统调用的处理流程,以查出问题的root cause.很多书把它和中断处理放在一起讲,但是又没有哪本书说清楚了,看来只有代码才能说明一切.以 ...

  2. 颜色渐变的RGB计算

    均匀渐变 渐变(Gradient)是美学中一条重要的形式美法则,与其相对应的是突变.形状.大小.位置.方向.色彩等视觉因素都可以进行渐变.在色彩中,色相.明度.纯度也都可以产生渐变效果,并会表现出具有 ...

  3. javascript ~~ 符号的使用

    其实是一种利用符号进行的类型转换,转换成数字类型 大概是这样滴: ~~true == 1 ~~false == 0 ~~"" == 0 ~~[] == 0 ~~undefined ...

  4. linux创建用户

    创建用户   sudo adduser xxx 删除用户   sudo userdel xxx 删除用户和目录  sudo userdel -r xxx

  5. php验证是否为手机端还是PC

    <?php $forasp = strtolower($_SERVER['HTTP_USER_AGENT']); if(strpos($forasp,'mobile')==true) { ech ...

  6. C#中邮件的发送基本操作

    本地配置的邮箱:http://localhost:6080/index.php //邮件的收发需要用到两个类   //1.用来创建一封邮件对象     //1.MailMessage 添加对 usin ...

  7. 【转】 IOS,objective_C中用@interface和 @property 方式声明变量的区别

    原文: http://blog.csdn.net/ganlijianstyle/article/details/7924446 1.在  @interface :NSObject{} 的括号中,当然N ...

  8. [转]mysql导出导入中文表解决方法

    在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下. 在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下一.先针 ...

  9. hbase的存储体系

    一.了解hbase的存储体系. hbase的存储体系核心的有Split机制,Flush机制和Compact机制. 1.split机制 每一个hbase的table表在刚刚开始的时候,只有一个regio ...

  10. 【POJ2761】【fhq treap】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...