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输出最小字典序又加深了一些 ...
随机推荐
- iOS 8 设置导航栏的背景颜色和背景图片
假设是storyboard 直接embed一个导航栏.然后在新出现的导航栏 选属性 选一下颜色就能够了 代码实现背景颜色改动:self.navigationController.navigationB ...
- Notepad++如何安装并使用source code pro 字体 转
http://blog.yucanlin.cn/2015/04/08/linux-%E5%AE%89%E8%A3%85-source-code-pro-%E5%AD%97%E4%BD%93/ ht ...
- [转] Android学习系列(29)--App调试的几个命令实践
在Android的应用开发中,我们会用到各种代码调试:其实在Android的开发之后,我们可能会碰到一些随机的问题,如cpu过高,内存泄露等,我们无法简单的进行代码调试,我们需要一个系统日志等等,下面 ...
- Struts2 result type
Struts2支持的不同类型的返回结果为: type name 说明 dispatcher 缺省类型,用来转向页面,通常处理JSP chain 转向另一个action,用来处理Action链 redi ...
- Linux 用户与用户组
1 Linux 用户与组 Linux是一个多用户多任务操作系统.多用户可以在同一时间内登录同一个系统执行各自不同的任务,而互不影响. 1.1 Linux下用户的角色分类 Linux用户的角色分为3 ...
- PHP替换数据库的换行符
//php 有三种方法来解决 //1.使用str_replace 来替换换行 $str = str_replace(array("\r\n", "\r", &q ...
- maven jetty运行命令
1.先运行build.xml <?xml version="1.0" encoding="UTF-8"?> <project name=&qu ...
- 判断浏览器是否支持FileReader
1.js代码: //判断浏览器是否支持FileReader if (typeof FileReader == "undefined") { document.write(" ...
- C# DataTable去除重复,极其简便、简单
其中sourceDT是获取到的一个DataTable类型的集合对象 去重复使用方式: 实例化一个DataView对象 假设为dv,直接dv.ToTable()即可,ToTable中可为(true,&q ...
- Deep Learning 学习随记(五)Deep network 深度网络
这一个多周忙别的事去了,忙完了,接着看讲义~ 这章讲的是深度网络(Deep Network).前面讲了自学习网络,通过稀疏自编码和一个logistic回归或者softmax回归连接,显然是3层的.而这 ...