题意:用一个字符串表示树,0代表向下走,1代表往回走,求两棵树是否同构。

分析:同构的树经过最小表示会转化成两个相等的串。

方法:递归寻找每一棵子树,将根节点相同的子树的字符串按字典序排列,递归回去即可。最终得到的串将是这棵树的最小表示。

举例:0010011101001011,表示的树如下

根节点下的三棵子树分别为00100111、01、001011

大的原则是:递归过程中,每次都将当前这棵树的所有子树的字符串排序。

00100111这棵子树,将它的根结点设为S,它的子树是01和0011,那么应该在S处将这两棵子树按字典序排列,结果是001101,而最终递归结束返回的结果是00011011(在001101的前面加了个0,后面加了个1),递归的作用是更新每一棵子树,使其按照最小字典序的字符串表示,这样处理,会使同构的树经过最小表示转化成两个相等的串。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) a < b ? a : b
#define Max(a, b) a < b ? b : a
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, };
const int dc[] = {-, , , };
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char a[MAXN], b[MAXN];
string dfs(string s){
vector<string> v;
int len = s.size();
int deep = , start = ;
for(int i = ; i < len; ++i){
if(s[i] == '') ++deep;
else --deep;
if(deep == ){
string tmp = s.substr(start + , i - start - );
if(tmp == ""){
v.push_back("");
}
else{
tmp = dfs(tmp);
v.push_back("" + tmp + "");
}
start = i + ;
}
}
sort(v.begin(), v.end());
string ans = "";
len = v.size();
for(int i = ; i < len; ++i){
ans += v[i];
}
return ans;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%s%s", a, b);
string s1 = dfs(a);
string s2 = dfs(b);
if(s1 == s2) printf("same\n");
else printf("different\n");
}
return ;
}

HDU 1954 Subway tree systems (树的最小表示法)的更多相关文章

  1. [POJ 1635] Subway tree systems (树哈希)

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

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

    Subway tree systems POJ - 1635 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树 /* 在po ...

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

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

  4. 【POJ】【1635】Subway Tree Systems

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

  5. POJ 1635 树的最小表示法/HASH

    题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...

  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. POJ1635 树的最小表示法(判断同构)

    Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, ther ...

  8. HDU 4162 Shape Number(字符串,最小表示法)

    HDU 4162 题意: 给一个数字串(length <= 300,000),数字由0~7构成,求出一阶差分码,然后输出与该差分码循环同构的最小字典序差分码. 思路: 第一步是将差分码求出:s[ ...

  9. [BZOJ4337][BJOI2015]树的同构(树的最小表示法)

    4337: BJOI2015 树的同构 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1023  Solved: 436[Submit][Status ...

随机推荐

  1. How to Copy and Paste in the Ubuntu Gnome Terminal

    How to Copy: Select the content in terminal use your mouse , and then use Ctrl + Shift + C to copy t ...

  2. C++ SFINAE

    1. 什么是SFINAE 在C++中有很多的编程技巧(Trick), SFINAE就是其中一种, 他的全义可以翻译为”匹配失败并不是一个错误(Substitution failure is not a ...

  3. MySQL中部分系统变量介绍

      have_symlink                             DISABLED                                   YES 用以支持在表定义中指 ...

  4. uva 12096 The SetStack Computer

    点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...

  5. archlinux随记

    xrdb -merge .Xresources才能使urxvt的配色显示正常 xpmroot 包含在fvwm包中

  6. LeetCode42 Trapping Rain Water

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  7. LeetCode4 Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  8. 1.5.1 Analyzers,Tokenizers,Filters概述

    字段分析器(Analyzers)即用于文档索引也用于查询.一个分析器检查字段的文本,并生成一个token流.分析器可能是一个单独的类,也可能是一系列的tokenizer和filter的组合. 分词器把 ...

  9. UIView 的transitionFromView方法实现视图切换

    #import "ViewController.h" @interface ViewController () @property (strong, nonatomic) IBOu ...

  10. nodejs 安装 postgresql module

    # npm -gd install node-gyp # export PATH=$PATH:/usr/local/pgsql/bin # npm -gd install pg for test: # ...