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. SDUTRescue The Princess(数学问题)

    题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To re ...

  2. DllImport中的EntryPoint

    工作中常常会使用到C#的应用来调用C++中的底层函数,此时就须要使用到DllImport,而DllImport中有一个EntryPoint(入口点),非常多文章都没有说明这个值怎样进行获取的,详细获取 ...

  3. [Unity3D]Unity3D游戏开发之自己主动寻路与Mecanim动画系统的结合

    大家好,欢迎大家关注我的博客,我是秦元培,我的博客地址是blog.csdn.net/qinyuanpei. 这段时间博主将大部分的精力都放在了研究官方演示样例项目上,主要是希望能够从中挖掘出有价值的东 ...

  4. Qt 学习之路:QML 基本元素

    QML 基本元素可以分为可视元素和不可视元素两类.可视元素(例如前面提到过的Rectangle)具有几何坐标,会在屏幕上占据一块显示区域.不可视元素(例如Timer)通常提供一种功能,这些功能可以作用 ...

  5. 解读dbcp自动重连那些事---转载

    http://agapple.iteye.com/blog/791943 可以后另一篇做对比:http://agapple.iteye.com/blog/772507 同样的内容,不同的描述方式,不一 ...

  6. Linux PATH变量的设置

    一般Linux系统,有两个配置文件可以设置PATH变量,一:.bashrc 二:.bash_profile; 还有一种方法可以临时设置PATH变量(三) 一:  1.编辑.bashrc,添加 expo ...

  7. C## 输出Hello world

    首先新建一个项目 然后在文件D:\C##Obj\HelloWorld\HelloWorld\Program.cs using System; using System.Collections.Gene ...

  8. 乐视手机查看运行内存方法、EUI(Eco User Interface)乐视系统查看手机运行内存方法

    点击按钮,左下角,方格, 显示如下:

  9. Android Studio下添加引用jar文件和so文件

    博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 安卓开发中我们常会遇到jar文件和so文件的引用,下面介绍下在as下如何添加使用,这里以百度地图s ...

  10. windows服务,安装、启动、停止,配置,一个批处理文件搞定

    相对而言,还是比较通用的吧,如果哪位仁兄有更好的实现方式,或者发现有不足之处,还请多多指教.  @echo off echo.------------------------------------- ...