题目链接:http://poj.org/problem?id=1635

题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯。

我刚开始自己设计了哈希函数,不知道为什么有问题。。。。参考了http://www.cnblogs.com/jackiesteed/articles/2065307.html,他的就是对的。。我也不知道为什么。。

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <ctime>
#include <cstdlib>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> vec;
typedef vector<vec> mat;
#define AA first
#define BB second
#define PB push_back int T,h[];
char a[],b[];
char *p; void init(){
srand(time(NULL));
for(int i=;i<;i++){
h[i] = rand()%;
}
} int calc_hash(int i){
int res = h[+i];
while(*p&&*p++==''){
res = (res + h[i]*calc_hash(i+))%;
}
return (res*res)%;
} int main(){
init();
scanf("%d",&T);
while( T-- ){
scanf("%s%s",a,b);
p = a;
int ha = calc_hash();
p = b;
int hb = calc_hash();
if( ha==hb ) puts("same");
else puts("different");
}
return ;
}

[POJ 1635] Subway tree systems (树哈希)的更多相关文章

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

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

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

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

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

    题意:一串01序列,从一个点开始,0表示去下一个点,1表示回到上一个点,最后回到起点,遍历这棵树时每条边当且仅当走2次(来回) 给出两串序列,判断是否是同一棵树的不同遍历方式 题解:我们把每一个节点下 ...

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

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

  5. 【POJ】【1635】Subway Tree Systems

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

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

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

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

  8. 【树哈希】poj1635 Subway tree systems

    题意:给你两颗有根树,判定是否同构. 用了<Hash在信息学竞赛中的一类应用>中的哈希函数. len就是某结点的子树大小,g是某结点的孩子数+1. 这个值也是可以动态转移的!具体见论文,所 ...

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

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

随机推荐

  1. Android 常遇错误解决方案

    遇到问题描述: 运行android程序控制台输出 [2012-07-18 16:18:26 - ] The connection to adb is down, and a severe error ...

  2. 关于SQLServer2005的学习笔记—异常捕获及处理

    转自:http://blog.csdn.net/baoqiangwang/article/details/5395874 SQLServer2005 提供了类似于 C# 和 C++ 语言中的异常处理的 ...

  3. Android学习笔记1 android adb启动失败问题 adb server is out of date. killing...

    下面是Android的学习笔记,原文地址. 我是使用adb devices出现如下红字错误, 使用第一种方法方法,结果关掉豌豆荚就可以了. android adb启动失败问题 adb server i ...

  4. [Hibernate] - Annotations - One To One

    Hibernate annotation 一对一的两种实现: 1)幅表中有主表的主键ID做为引用 2)幅表的主键即为主表的ID hibernate.cfg.xml <?xml version=& ...

  5. .net平台的RSA实现以及与Delphi之间的互操作性

    .net平台下面的RSA算法实现是RSACryptoServiceProvider,如果安装了 Microsoft Enhanced Cryptographic Provider,则 RSACrypt ...

  6. Silverlight开源框架SL提供便捷的二次开发银光框架

    Silverlight开发框架SilverFrame欢迎咨询 基于Silverlight4.0开发,兼容Silverlight 5.0,SQLServer2005数据库.WCF: 本框架有清爽的前端界 ...

  7. 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory

    关于程序路径 LucenePath:@(System.Configuration.ConfigurationManager.AppSettings["LucenePath"])&l ...

  8. LitDB笔记

    首先,LitDB存储的不是json,而是bson.这里就有问题了,json是可以无限扩展的,但是bson不行.所有insert的时候需要使用实体类而不是hashtable.但是它又提供了一个叫做Bso ...

  9. Redis报错:WRONGTYPE Operation against a key holding the wrong kind of value 解决处理

    首先应该明白报这个错误说明了你用的jedis方法与redis服务器中存储数据的类型存在冲突. 例如:数据库中有一个key的数据存储的是Hash类型的,但是你使用jedis执行数据操作的时候却使用了非H ...

  10. find_in_set mysql

    有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1,3,4的格式存 ...