题意:用一个字符串表示树,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. AssetsManager下载类

    cocos2dx-2.1.3 2dx自己代的例子进行讲解 360   cocos2dx net  --> 2.1.3AssetsManager AppDelegate.cpp详解 1.创建目录 ...

  2. 永远不要在Linux 执行的 10 个最危险的命令

    转: http://www.tecmint.com/10-most-dangerous-commands-you-should-never-execute-on-linux/ http://www.l ...

  3. 添加IFrame导致内存溢出的解决过程(IE浏览器,目前发现了原因,还未解决)

    1.  现象 每次动态添加iframe时,iexplore.exe进程占据的内存都会增加(大概10M左右),不会自动释放,最终导致内存溢出 2.  解决过程 经过网络的一番搜索,基本上给出的解决方案是 ...

  4. D. Ilya and Escalator

    D. Ilya and Escalator time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  6. NSDate简单介绍

    NSDate简单介绍 一:NSDate是一个日期\时间方面的类,主要用来创建\获取时间 1.NSDate对象的创建: date 创建一个当前系统日期和时间的对象 dateWithTimeInterva ...

  7. web前端开发(1)

    web标准 结构标准.样式标准.行为标准分离 结构标准:xml xhtml  html 样式标准:css 行为标准:DOM  ECMAScript 符合标准的网页:1.标签小写  2.属性加引号 3. ...

  8. Shell学习笔记 - 条件判断式

    1. 判断格式 1) test 参数 文件 例: test -e /root/install.log 2) [ 参数 文件 ]  -- 推荐使用 例: [ -e /root/install.log ] ...

  9. node.js学习的资源整理

    node中文社区 Node.js专业中文社区:https://cnodejs.org/ node文档 node.js 中文api :http://nodeapi.ucdok.com/ node.js入 ...

  10. 从Setting.settings到Resource.resx

    之前由于经验不足,将常用的App提示信息串(string)放置在了配置文件中(*.Settings).目前需要将App国际化,对这些信息的翻译有两个途径: 直接翻译,将参数中的提示信息串用英文或者其他 ...