Description

Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, there is one and only one way of going by subway. Moreover, most of these cities have a unique central station. Imagine you are a tourist in one of these cities
and you want to explore all of the subway system. You start at the central station and pick a subway line at random and jump aboard the subway car. Every time you arrive at a station, you pick one of the subway lines you have not yet travelled on. If there
is none left to explore at your current station, you take the subway line back on which you first came to the station, until you eventually have travelled along all of the lines twice,once for each direction. At that point you are back at the central station.
Afterwards, all you remember of the order of your exploration is whether you went further away from the central station or back towards it at any given time, i.e. you could encode your tour as a binary string, where 0 encodes taking a subway line getting you
one station further away from the central station, and 1 encodes getting you one station closer to the central station. 



Input

On the first line of input is a single positive integer n, telling the number of test scenarios to follow.Each test scenario consists of two lines, each containing a string of the characters '0' and '1' of length at most 3000, both describing a correct exploration
tour of a subway tree system.

Output

exploration tours of the same subway tree system, or the text "different" if the two strings cannot be exploration tours of the same subway tree system.

Sample Input

2
0010011101001011
0100011011001011
0100101100100111
0011000111010101

Sample Output

same
different

事实上题目意思就是求两个有根树是否同构,这个假设暴力法枚举的话。复杂度是O(N^2)。一中经典的做法是哈希。思想就是使得不同结构的树哈希值不同。而同构的树哈希值同样。

我这个也是參考别人写的。不同的是我先把01串转换为了树状结构表示。然后再递归求哈希值,这样好理解一点。

哈希的策略:先随机产生一系列随机数作为存到数组。接着从根节点出发。递归计算每一个子树的哈希值,将子树的哈希值相加然后和父节点自己相应的数组上的随机数相加得到父节点的哈希值。这个计算结果和子树的顺序是没有关系的,所以同构的树一哈希值一定是一样的。

对于异构的树,必定在某些节点计算的哈希值不同,因为都是随机产生的一些数字,所以他们相加值和另外一棵树哈希值同样的概率也会很低。(应该不能全然保证的,这里我们加了个取模m的操作。依据鸽巢原理,当树的数量超过m,必定有两个树的哈希值是会同样的,但这两个树却未必是同构的,不知道大家认为对不正确?)

import java.util.*;
public class SubwayTreeSstems1635 { static final int Hn=11000;
static int h[]=new int[Hn];
static Random rand=new Random(System.currentTimeMillis());
static int m=1000000007;
static int index=0;
/**
* @param args
*/
public static void main(String[] args) { run();
} private static void init() { for(int i=0;i<Hn;i++)
h[i]=(rand.nextInt()%m);
} public static void run()
{
Scanner in=new Scanner(System.in);
int T=in.nextInt();
init();
for(int t=0;t<T;t++)
{
String s1=in.next();
Node tree1=createTree(s1);
String s2=in.next();
Node tree2=createTree(s2);
/*System.out.println(tree1.children.size()+" "+tree2.children.size());
displayTree(tree1);
System.out.println();
displayTree(tree2);*/ int a=hash(tree1,1);
int b=hash(tree2,1);
//System.out.println(a+" "+b);
if(a==b)
{
System.out.println("same");
}
else
{
System.out.println("different");
}
}
} public static int hash(Node tree,int j)
{
int sum=h[j+5000];//j是树的高度
for(Node n:tree.children)
sum=(sum+h[j]*hash(n,j+1))%m;//把子树的哈希值加到父节点上去
return (sum*sum)%m; } private static Node createTree(String s) { char[] seq=s.toCharArray();
Node root=new Node(0);
Node p=root;
int index=1;
for(int i=0;i<seq.length;i++)
{
if(seq[i]=='0')
{
Node node =new Node(index++);
connect(p,node);
p=node;
}
else if(seq[i]=='1')
{
p=p.parent;
}
}
//if(p==root)
// System.out.println("create success!");
return root;
} private static void connect(Node p, Node node) { node.parent=p;
p.children.add(node);
} public static void displayTree(Node tree)
{
System.out.println(tree);
for(Node ch:tree.children)
displayTree(ch);
} } class Node
{
int id;
Node parent=null;
List<Node> children=new ArrayList<Node>();
public Node(int n)
{
id=n;
}
public String toString()
{
StringBuilder sb=new StringBuilder();
sb.append(id).append(": ");
for(Node n:children)
sb.append(n.id).append(" ");
return sb.toString();
}
}

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 (树哈希)

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

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

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

  5. 【POJ】【1635】Subway Tree Systems

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

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

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

  7. 【树哈希】poj1635 Subway tree systems

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

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

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

  9. POJ1635:Subway tree systems

    链接:http://poj.org/problem?id=1635 填坑树同构 题目给出的是除根外的括号序列表示. 其实只要跟你说hash大家都能写得出来…… hash函数取个效果别太差的就行了吧 # ...

随机推荐

  1. Hibernate框架之HQL查询与Criteria 查询的区别

    Hibernate框架提供了HQL查询和Criteria 查询.下面对这两种查询分别做个例子.也好对这两种查询方法有个大概的了解.就用房屋信息表做例子,查询所有房屋信息. HQL语句查询所有房屋信息: ...

  2. jsp学习笔记 - 内置对象 config

    1.将页面保存在 WEB-INF文件夹下是最安全的,一般不可见 可以通过映射路径来进行访问 2.通过config对象可以取得初始化的配置参数 String dbDriver = config.getI ...

  3. 用rownum先排序后分页

    今天突然想到rownum可以解决分页问题,于是做了各种实验,找个几个文章,最后有了一定成果. 现有表tablename,含有字段showorder,要求提取showorder的第11行到20行数据. ...

  4. MySql学习笔记(三) —— 聚集函数的使用

    1.AVG() 求平均数 select avg(prod_price) as avg_price from products; --返回商品价格的平均值 ; --返回生产商id为1003的商品价格平均 ...

  5. Shell输入/输出重定向

    输出重定向 重定向一般通过在命令间插入特定的符号来实现.特别的,这些符号的语法如下所示 command1 >file1 上面这个命令执行command1然后将输出的内容存入file1. 注意任何 ...

  6. Redis系列(九)--几道面试题

    这里只是一点面试题,想了解更多,可以查看本人的Redis系列:https://www.cnblogs.com/huigelaile/category/1461895.html 1.Redis和Memc ...

  7. 02C++基本语法

    基本语法 2.1.1单行注释 // 2.1.2多行注释 /* * */ 2.1.3标识符 C++ 标识符是用来标识变量.函数.类.模块,或任何其他用户自定义项目的名称.一个标识符以字母 A-Z 或 a ...

  8. 官方安装docker-ce步骤

    这里是Centos7安装方式 安装依赖包 $ sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 添加Docker ...

  9. (2) GoJS Node简介

    node GoJS提供了非常简单的创建Node节点的方法,可将文本内容.结点形状.背景颜色.边距等属性通过数据绑定[go.Binding]直接绑定到对应的Node数据中. 本文简单介绍Node的创建过 ...

  10. Re0:DP学习之路 母牛的故事 HDU - 2018

    解法 一定要注意斐波那契数列的原始意义,斐波那契数列也叫作兔子数列是兔子繁衍的一种表示方法.同样适用于别的情况的动物繁衍问题 原始的是3个月一胎现在四个月那么方程就是 f(n)=n n<=4 f ...