题意:一串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 (树的最小表示法)的更多相关文章

  1. HDU 1954 Subway tree systems (树的最小表示法)

    题意:用一个字符串表示树,0代表向下走,1代表往回走,求两棵树是否同构. 分析:同构的树经过最小表示会转化成两个相等的串. 方法:递归寻找每一棵子树,将根节点相同的子树的字符串按字典序排列,递归回去即 ...

  2. [POJ 1635] Subway tree systems (树哈希)

    题目链接:http://poj.org/problem?id=1635 题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯. 我刚开始自己设计了哈希函数,不知道为什么有问题.. ...

  3. poj 1635 Subway tree systems(树的最小表示)

    Subway tree systems POJ - 1635 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树 /* 在po ...

  4. POJ 1635 Subway tree systems 有根树的同构

    POJ 1635 题目很简单 给个3000节点以内的根确定的树 判断是否同构.用Hash解决,类似图的同构,不过效率更高. #include<iostream> #include<c ...

  5. 【POJ】【1635】Subway Tree Systems

    树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...

  6. POJ1635 Subway tree systems ——(判断树的同构,树的最小表示法)

    给两棵有根树,判断是否同构.因为同构的树的最小表示法唯一,那么用最小表示法表示这两棵树,即可判断同构.顺便如果是无根树的话可以通过选出重心以后套用之前的方法. AC代码如下: #include < ...

  7. POJ 1635 树的最小表示法/HASH

    题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...

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

  9. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

随机推荐

  1. windows下编译boost for android

    env: windows xp 32 bit mingw official  NDK 1. 下载源代码    地址是 :http://sourceforge.net/projects/boost/fi ...

  2. [Spring Data MongoDB]学习笔记--注册一个Mongo实例

    1. 通过Java based bean metadata @Configuration public class AppConfig { public @Bean Mongo mongo() thr ...

  3. 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

    [BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...

  4. 【BZOJ2819】Nim 树状数组+LCA

    [BZOJ2819]Nim Description 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可 ...

  5. LIS(模板)

    记录一下,O(nlgn)的算法求LIS //HHH #include <iostream> #include <stdio.h> #include <string.h&g ...

  6. Java前端Rsa公钥加密,后端Rsa私钥解密(支持字符和中文)

    Base64工具类,可以让rsa编码的乱码变成一串字符序列 package com.utils; import java.io.ByteArrayInputStream; import java.io ...

  7. wamp设置mysql默认编码

    来自:http://www.cnsecer.com/5984.html wamp下MySQL的默认编码是Latin1,不支持中文,要支持中文的话需要把数据库的默认编码修改为gbk或者utf8. 这里推 ...

  8. 华盟网公开课 office 宏 主讲Alex

    powershell github: veil-evasion macroshop   unicorn https://github.com/trustedsec/unicorn metasploit ...

  9. Spring 事务机制详解(事务的隔离性和传播性)

    原文出处: 陶邦仁 Spring事务机制主要包括声明式事务和编程式事务,此处侧重讲解声明式事务,编程式事务在实际开发中得不到广泛使用,仅供学习参考. Spring声明式事务让我们从复杂的事务处理中得到 ...

  10. k8s 常用命令

    [root@master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 1h v1.8.1 node1 ...