Hash method to determine whether two root trees are isomorphic

Source: Internet
Author: User
Tags hash connect rand tostring

Description

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

Input

On the (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, b Oth 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 is exploration tours O f the same subway tree system.

Sample Input

2
0010011101001011
0100011011001011
0100101100100111
0011000111010101

Sample Output

Same
different

In fact, the title is to ask two have a root tree is isomorphic, if the violence of the enumeration, the complexity is O (n^2), one of the classic approach is a hash, the idea is to make the different structure of the tree hash value, and the homogeneous tree hash value is the same.

I am also referring to other people to write, the difference is that I first 01 string conversion for the tree structure, and then recursive hash value, so that a little understanding.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Hash strategy: First randomly generate a series of random numbers as stored in the array, and then from the root node, recursively compute the hash of each subtree, add the hash of the subtree and then the parent node of the corresponding array of random numbers added to the parent node's hash value. The result of this calculation is not related to the order of the subtree, so the homogeneous tree hash value must be the same. For heterogeneous trees, the hash values that are necessarily computed at some nodes are different, and because they are random numbers, the probability of their addition being the same as another tree hash is very low. (should not be fully guaranteed, here we add a modulo m operation, according to the pigeon Nest principle, when the number of trees is more than m, there must be two of the hash of the tree will be the same, but the two trees are not necessarily isomorphic, do not know that everyone think right? )

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.