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

Subway tree systems

POJ - 1635

题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树

/*
    在poj上交需要加一个string头文件,不然会CE
    树的最小表示
    这里用的最小表示法就是将树的所有子树分别用1个字符串表示,要按字典序排序将他们依依连接起来。连接后如果两个字符串是一模一样的,那么他们必然是同构的。这样原问题就变成了子问题,子树又是一颗新的树。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
string s1,s2;
string mn(string str){//得到树的最小表示
    int dep=0,st=0;
    vector<string>a;
    string stemp;
    for(int i=0;i<str.size();i++){
        dep+=str[i]==‘1‘?-1:1;
        if(!dep){
            stemp="0"+mn(str.substr(st+1,i-st))+"1";
            a.push_back(stemp);
            st=i+1;
        }
    }
    sort(a.begin(),a.end());
    stemp=string("");
    for(int i=0;i<a.size();i++)stemp+=a[i];
    return stemp;
}
int main(){
    freopen("Cola.txt","r",stdin);
    int T;scanf("%d",&T);
    while(T--){
        cin>>s1>>s2;
        string ss1=mn(s1);
        string ss2=mn(s2);
        if(ss1==ss2)puts("same");
        else puts("different");
    }
}
时间: 2024-08-04 22:42:36

poj 1635 Subway tree systems(树的最小表示)的相关文章

[有向树的最小表示] poj 1635 Subway tree systems

题目链接: http://poj.org/problem?id=1635 Subway tree systems Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6541   Accepted: 2747 Description Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, th

POJ 1635 Subway tree systems Hash法判断有根树是否同构

Hash在信息学竞赛中的一类应用 中的某道例题 "不难想到的算法是使用两个字符串分别表示两棵树,但是如果使用Hash的话应该怎么做呢? 可以使用一种类似树状递推的方法来计算Hash值:  对于一个节点v,先求出它所有儿子节点的Hash值,并从小到大排序,记作H1,H2,„,HD.那么v的Hash值就可以计算为:   (((a * p) ^ H1 mod q) * p ^ H2 mod q).....  换句话说,就是从某个常数开始,每次乘以p,和一个元素异或,再除以q取余,再乘以p,和下一个元素

[ POJ ][ HASH ] 1635 Subway tree systems

首先,对于一个树,我们可以有一种压缩表示: 0010011101001011 其中 0 表示向下走,1 表示向上走.于是: 00100111|01|001011 对于每一段的 0 1 出现次数相同,这种hash方法叫 树的最小表示法 . 1635 题目精简大意:给你n对01字符串,判断每一对儿表示的是不是同一个树,方法: 1.定义 cnt, start, end 来记录当前0 1之和是否相等,start,end 记录相等时所得字数的范围. 2.去首尾得到子串,递归进行1步骤直到子串只为“0 1”

【POJ】【1635】Subway Tree Systems

树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同构的,思路是用树的最小表示法去做.这里用的最小表示法就是将树的所有子树分别用1个字符串表示,要按字典序排序将他们依依连接起来.连接后如果两个字符串是一模一样的,那么他们必然是同构的.这样原问题就变成了子问题,子树又是一颗新的树. 1 Source Code 2 Problem: 1635 User:

poj 3321:Apple Tree(树状数组,提高题)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

poj-1635 Subway tree systems(推断两个有根树是否同构)-哈希法

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 o

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

POJ - 3321 Apple Tree Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very muc

POJ 3321 Apple Tree 【树状数组+建树】

题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34812 Accepted: 10469 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka like

(简单) POJ 3321 Apple Tree,树链剖分+树状数组。

Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches.