POJ 1635 Subway tree systems (树的最小表示法)
题意:一串01序列,从一个点开始,0表示去下一个点,1表示回到上一个点,最后回到起点,遍历这棵树时每条边当且仅当走2次(来回)
给出两串序列,判断是否是同一棵树的不同遍历方式
题解:我们把每一个节点下个每棵子树形成的01序列排序(我们把01序列看做括号序列,0看做'(', 1看做‘)’,则就是把每个并列的括号序列排序),再比较得出的字符串就好。
方法:把不标准的东西标准化,再比较。注意我们要保证标准化是唯一的
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const ll INF=1ll<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
char str[Max];
string dfs(int now)
{
vector<string> vec;
while(str[now]=='')//可看做‘(’
{
vec.push_back(''+dfs(now+));
now+=vec.back().size();//末尾元素的长度
}
string tem;
sort(vec.begin(),vec.end());//将01序列进行排序
int m=vec.size();
for(int i=;i<m;++i)
tem+=vec[i];
return tem+'';//看做‘)’
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
string op1=dfs();
scanf("%s",str);
string op2=dfs();
if(op1==op2)
printf("same\n");
else
printf("different\n");
}
return ;
}
POJ 1635 Subway tree systems (树的最小表示法)的更多相关文章
- HDU 1954 Subway tree systems (树的最小表示法)
题意:用一个字符串表示树,0代表向下走,1代表往回走,求两棵树是否同构. 分析:同构的树经过最小表示会转化成两个相等的串. 方法:递归寻找每一棵子树,将根节点相同的子树的字符串按字典序排列,递归回去即 ...
- [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 ...
- POJ 1635 Subway tree systems 有根树的同构
POJ 1635 题目很简单 给个3000节点以内的根确定的树 判断是否同构.用Hash解决,类似图的同构,不过效率更高. #include<iostream> #include<c ...
- 【POJ】【1635】Subway Tree Systems
树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...
- POJ1635 Subway tree systems ——(判断树的同构,树的最小表示法)
给两棵有根树,判断是否同构.因为同构的树的最小表示法唯一,那么用最小表示法表示这两棵树,即可判断同构.顺便如果是无根树的话可以通过选出重心以后套用之前的方法. AC代码如下: #include < ...
- 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 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
随机推荐
- iPhone程序中的加密处理
本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6650478 原文链接 : http://www.yifeiyang.ne ...
- 巨蟒python全栈开发django10:ajax&&登录认证
通过题目进行知识点回顾: 聚合查询 From django.db.models import Avg,Min,Max,F,Q,Count,Sum #查询书籍的平均值 Ret= Models.Book. ...
- 为什么调用 GdiplusShutdown 函数会在 DllExports::GdipDeleteGraphics(nativeGraphics) 位置抛出异常?
因为没有仔细看文档 https://docs.microsoft.com/en-us/windows/desktop/api/Gdiplusinit/nf-gdiplusinit-gdiplusshu ...
- [BZOJ3551]Peaks
[BZOJ3551]Peaks BZOJ luogu 建Kruskal重构树,点权为边权 按dfn序建出主席树 倍增找到能跳到的最浅的祖先 主席树查询一下 #include<bits/stdc+ ...
- 常用的JS代码块收集
/**数组去重一*/ (function (arr) { arr = arr.sort(); for (var i = 0; arr[i]; i++) { if (arr[i] === arr[i + ...
- 内置函数:min 用法
内置函数:min 用法 源码 def min(*args, key=None): # known special case of min """ min(iterable ...
- PAT 1002. A+B for Polynomials (25)
This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file con ...
- 爬虫四 selenium模块详细参数
selenium元素定位方法 一.访问页面并获取网页html from selenium import webdriver browser = webdriver.Chrome() browser.g ...
- 在html中插入音频
在html中插入音频 第一种:在页面代码中的<head></head>之间加入<bgsound src="音乐url" loop="-1&q ...
- python之路 正则表达式,模块导入的方法,hashlib加密
一.正则表达式re python中re模块提供了正则表达式相关操作 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的 ...