题目链接: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. struts2.0的工作原理

    struts2并不是一个陌生的web框架,它是以Webwork的设计思想为核心,吸收struts1的优点,可以说 struts2是struts1和Webwork结合的产物. struts2 的工作原理 ...

  2. SET Transaction Isolation Level Read语法的四种情况

    转自:http://www.cnblogs.com/qanholas/archive/2012/01/04/2312152.html 存储过程:SET Transaction Isolation Le ...

  3. Android 使用Telephony API

    Android 使用Telephony API public class TelephonyDemo extends Activity { TextView textOut; TelephonyMan ...

  4. [oracle] 解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed

    Windows 64位下装Oracle 11g 64位,PLSQL Developer使用出现以下问题: 1.Database下拉框为空: 2.强制输入用户名.密码及Database,登录弹出: In ...

  5. 【linux】 /etc/shadow 文件

    格式:username: passwd: lastchg: min: max: warn: inactive: expire: flag 登录名:加密口令:最后一次修改时间:最小时间间隔:最大时间间隔 ...

  6. Lumen 设置 timezone 时区

    Lumen 设置 timezone 时区 2015-06-19| wid| 后端开发 今天用 Lumen 框架写代码时, 也是初次体验 Lumen, 遇到了一个问题, 从数据库里查出的时间比数据库里保 ...

  7. Remove LUN from OCFS2

    # ocfs2_hb_ctl -I -d /dev/mapper/backup2-backup2 508C2154503344E78ACD6CD6B000000: 2 refs #The previo ...

  8. 微软为Visual Studio开发助手拓展C++支持

    近日,微软宣布了一项 Visual Studio“开发助手”(Developer Assistant)插件的重大更新,其现已支持“基于 C++ 的情境感知 web 解决方案”.开发助手能够嵌入 Vis ...

  9. 99. Recover Binary Search Tre

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  10. 访问修饰符internal

    internal(C# 参考) internal 关键字是类型和类型的成员 访问修饰符. 只有在同一程序集的文件中,内部类型或成员才是可访问的,如下例所示: public class BaseClas ...