[有向树的最小表示] 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, 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

Source

Northwestern Europe 2003

[Submit]   [Go Back]   [Status]  
[Discuss]

题目意思:

判断两棵树是否为同构的。0表示往前一步,1表示往后一步,从根出发。

解题思路:

有向树的最小表示。

对每个节点的每颗子树表示的字符串排序汇总。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

string a,aa,bb;
int n;

string dfs(int i)
{
    vector<string>myv;

    while(i<n&&a[i]==‘0‘) //说明是子树
    {
        myv.push_back(‘0‘+dfs(i+1));
        i+=myv.back().size(); //加上子树占用的位置,每次加一颗子树
    }
    sort(myv.begin(),myv.end()); //所有子树排序
    string res;

    for(int i=0;i<myv.size();i++) //连成一个整体
        res+=myv[i];
    return res+‘1‘;  //返回到当前树

}
int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   int t;

   scanf("%d",&t);
   while(t--)
   {
       cin>>a;

       n=a.size();
       aa=dfs(0);

       cin>>a;
       n=a.size();
       bb=dfs(0);

       if(aa==bb)
            printf("same\n");
       else
            printf("different\n");
   }
   return 0;
}

[有向树的最小表示] poj 1635 Subway tree systems,布布扣,bubuko.com

时间: 2024-08-06 16:06:46

[有向树的最小表示] poj 1635 Subway tree systems的相关文章

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

Subway tree systems POJ - 1635 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树 /* 在poj上交需要加一个string头文件,不然会CE 树的最小表示 这里用的最小表示法就是将树的所有子树分别用1个字符串表示,要按字典序排序将他们依依连接起来.连接后如果两个字符串是一模一样的,那么他们必然是同构的.这样原问题就变成了子问题,子树又是一颗新的树. */ #include<iostream>

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

POJ1635Subway tree systems

Subway tree systems Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8049   Accepted: 3357 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

poj 1635

有根树同构.参考论文<hash在....> 1 #include <iostream> 2 #include <fstream> 3 #include <algorithm> 4 #include <cstring> 5 #include <climits> 6 #include <cmath> 7 8 using namespace std; 9 10 const int leaf_hash=2099; 11 const

POJ 2499 Binary Tree 题解

本题使用所谓的辗转相除法. 还需要逆过来遍历二叉树.可以想象给出的数据点是根节点,然后遍历到根节点(1,1). 考的是根据给出的规则,总结规律的能力. #include <stdio.h> namespace BinaryTree2499_1 { int main() { int T, a, b, le, ri; scanf("%d", &T); for (int t = 1; t <= T; t++) { scanf("%d %d", &

[最小表示] poj 1509 Glass Beads

题目链接: http://poj.org/problem?id=1509 Glass Beads Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 2311   Accepted: 1343 Description Once upon a time there was a famous actress. As you may expect, she played mostly Antique Comedies most of