POJ 1635 Subway tree systems (树的最小表示法)
题意:一串01序列,从一个点开始,0表示去下一个点,1表示回到上一个点,最后回到起点,遍历这棵树时每条边当且仅当走2次(来回)
给出两串序列,判断是否是同一棵树的不同遍历方式
题解:我们把每一个节点下个每棵子树形成的01序列排序(我们把01序列看做括号序列,0看做'(', 1看做‘)’,则就是把每个并列的括号序列排序),再比较得出的字符串就好。
方法:把不标准的东西标准化,再比较。注意我们要保证标准化是唯一的
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const ll INF=1ll<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
char str[Max];
string dfs(int now)
{
vector<string> vec;
while(str[now]=='')//可看做‘(’
{
vec.push_back(''+dfs(now+));
now+=vec.back().size();//末尾元素的长度
}
string tem;
sort(vec.begin(),vec.end());//将01序列进行排序
int m=vec.size();
for(int i=;i<m;++i)
tem+=vec[i];
return tem+'';//看做‘)’
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
string op1=dfs();
scanf("%s",str);
string op2=dfs();
if(op1==op2)
printf("same\n");
else
printf("different\n");
}
return ;
}
POJ 1635 Subway tree systems (树的最小表示法)的更多相关文章
- HDU 1954 Subway tree systems (树的最小表示法)
题意:用一个字符串表示树,0代表向下走,1代表往回走,求两棵树是否同构. 分析:同构的树经过最小表示会转化成两个相等的串. 方法:递归寻找每一棵子树,将根节点相同的子树的字符串按字典序排列,递归回去即 ...
- [POJ 1635] Subway tree systems (树哈希)
题目链接:http://poj.org/problem?id=1635 题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯. 我刚开始自己设计了哈希函数,不知道为什么有问题.. ...
- poj 1635 Subway tree systems(树的最小表示)
Subway tree systems POJ - 1635 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树 /* 在po ...
- POJ 1635 Subway tree systems 有根树的同构
POJ 1635 题目很简单 给个3000节点以内的根确定的树 判断是否同构.用Hash解决,类似图的同构,不过效率更高. #include<iostream> #include<c ...
- 【POJ】【1635】Subway Tree Systems
树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...
- POJ1635 Subway tree systems ——(判断树的同构,树的最小表示法)
给两棵有根树,判断是否同构.因为同构的树的最小表示法唯一,那么用最小表示法表示这两棵树,即可判断同构.顺便如果是无根树的话可以通过选出重心以后套用之前的方法. AC代码如下: #include < ...
- POJ 1635 树的最小表示法/HASH
题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...
- poj-1635 Subway tree systems(推断两个有根树是否同构)-哈希法
Description Some major cities have subway systems in the form of a tree, i.e. between any pair of st ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
随机推荐
- CodeForces 213 E
/* 线段树 + hash: 首先我们可以知道A序列是1~n的排列,那么我们可以先在B序列中把1~n的排列找出来,看其相对位置是否与A相同(hash可做),相同即表明存在一个d满足条件. 以此类推,我 ...
- conda 和 pip 安装,更新,删除
安装: pip install xxx conda install xxx 更新: pip install --upgrade xxx conda update xxx 删除: pip uninst ...
- git commit -a -m "M 1、引入mixin,公共样式mixin传参处理;";git push origin master:master
<script> import wepy from 'wepy' import api from '../api/api' export default class recharge ex ...
- 初学习-python打印乘法表、正方形、三角形
for x in range(1,4): for o in range(0,x-1): print('*',end='') pass pass print('*') print('\n')print( ...
- JavaScript数据结构与算法-数组练习
一. 创建一个记录学生成绩的对象,提供一个添加成绩的方法,以及一个显示学生平均成绩的方法. // 创建一个记录学生成绩的对象 const Students = function Students () ...
- 通过天天模拟器加burpsuite抓取手机app流量
通过天天模拟器,代理抓取安卓app数据包.也可以抓取https. 1.下载天天模拟器,官方下载即可,下载安装. 2.启动天天模拟器,设置代理,点击上方wlan设置图标,打开wlan设置,如下: 3.鼠 ...
- MySQL中Cardinality值的介绍
1) 什么是Cardinality 不是所有的查询条件出现的列都需要添加索引.对于什么时候添加B+树索引.一般的经验是,在访问表中很少一部分时使用B+树索引才有意义.对于性别字段.地区 ...
- Python3.6全栈开发实例[026]
27.文件a.txt内容:每一行内容分别为商品名字,价钱,个数.apple 10 3tesla 100000 1mac 3000 2lenovo 30000 3chicken 10 3通过代码,将其构 ...
- python多进程理论
什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): 你在一个时间段内有很多任务要做:python学习的任务,赚钱的任务,交女朋 ...
- make menuconfig 时出现 mixed implicit and normal rules: deprecated syntax
這是 make 的版本問題!不清楚為何要這樣限制? 將此行 config %config: scripts_basic outputmakefile FORCE改成 ...