HDU 1954 Subway tree systems (树的最小表示法)
题意:用一个字符串表示树,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 (树的最小表示法)的更多相关文章
- [POJ 1635] Subway tree systems (树哈希)
题目链接:http://poj.org/problem?id=1635 题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯. 我刚开始自己设计了哈希函数,不知道为什么有问题.. ...
- poj 1635 Subway tree systems(树的最小表示)
Subway tree systems POJ - 1635 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树 /* 在po ...
- POJ1635 Subway tree systems ——(判断树的同构,树的最小表示法)
给两棵有根树,判断是否同构.因为同构的树的最小表示法唯一,那么用最小表示法表示这两棵树,即可判断同构.顺便如果是无根树的话可以通过选出重心以后套用之前的方法. AC代码如下: #include < ...
- 【POJ】【1635】Subway Tree Systems
树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...
- POJ 1635 树的最小表示法/HASH
题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...
- 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 ...
- POJ1635 树的最小表示法(判断同构)
Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, ther ...
- HDU 4162 Shape Number(字符串,最小表示法)
HDU 4162 题意: 给一个数字串(length <= 300,000),数字由0~7构成,求出一阶差分码,然后输出与该差分码循环同构的最小字典序差分码. 思路: 第一步是将差分码求出:s[ ...
- [BZOJ4337][BJOI2015]树的同构(树的最小表示法)
4337: BJOI2015 树的同构 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1023 Solved: 436[Submit][Status ...
随机推荐
- boost.asio源码剖析(二) ---- 架构浅析
* 架构浅析 先来看一下asio的0层的组件图. (图1.0) io_object是I/O对象的集合,其中包含大家所熟悉的socket.deadline_tim ...
- cocos2d-x学习之自动内存管理
一.自动内存管理 1)概述 C++语言默认是没有提供自动内存管理的.使用者需要自己分配,自己释放.在cocos2d-x里提供了一个自动内存管理的方案.主要是通过CCObject来提供的,用户只要继承了 ...
- 0c-34-自动释放池
#import <Foundation/Foundation.h> #import "Person.h" Person * creatPerson() { Person ...
- mysql服务器的常规操作
mysql服务器的常规操作 导读 MySQL是一个小型关系数据库管理系统,目前被广泛的应用在Internet上的中小型网站中,体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,使得许多中小型网站 ...
- Python Learning
这是自己之前整理的学习Python的资料,分享出来,希望能给别人一点帮助. Learning Plan Python是什么?- 对Python有基本的认识 版本区别 下载 安装 IDE 文件构造 Py ...
- Java最重要的21个技术点和知识点之JAVA集合框架、异常类、IO
(三)Java最重要的21个技术点和知识点之JAVA集合框架.异常类.IO 写这篇文章的目的是想总结一下自己这么多年JAVA培训的一些心得体会,主要是和一些java基础知识点相关的,所以也希望能分享 ...
- EF——默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射 02 (转)
EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射 I.EF里的默认映射 上篇文章演示的通过定义实体类就可以自动生成数据库,并且EF自动设置了数据库 ...
- 使用HttpURLConnection和AsyncTask从网络缓存图片
1.创建NetCacheUtils中创建downloadBitmap(String url)方法 private Bitmap downloadBitmap(String url){ HttpURLC ...
- SOA与C#
What is SOA? SOA or Service oriented architecture is an architecture style for building business app ...
- [Java] 遍历HashMap和HashMap转换成List的两种方式
遍历HashMap和HashMap转换成List /** * convert the map to the list(1) */ public static void main(String[] ...