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 题意:有根树,然后0表示访问儿子,1表示返回父亲。
题解:只需要将每个节点的子树大小求出,排序,判断是否一样就可以了,然而我是hash的,十分巧妙。
 #include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std; int l;
string a,b;
vector<int>e[];
unsigned ll H[];
int fa[]; bool cmp(int a,int b){return H[a]>H[b];} void dfs(int x)
{
H[x]=;
for(int i=;i<e[x].size();i++)
dfs(e[x][i]);
sort(e[x].begin(),e[x].end(),cmp);//为什么排序,因为根据有序来hash,不然是无法比较
for(int i=;i<e[x].size();i++)
H[x]=H[x]*+H[e[x][i]]*;
}
unsigned ll cal(string a)
{
l=a.length();
int now=,cnt=;
for(int i=;i<l;i++)
if(a[i]=='')
{
e[now].push_back(++cnt);
fa[cnt]=now,now=cnt;
}
else now=fa[now];
dfs();
for(int i=;i<=cnt;i++)
e[i].clear();
return H[];
}
int main()
{
int cas;scanf("%d",&cas);
while(cas--)
{
cin>>a;cin>>b;
if(cal(a)==cal(b))puts("same");
else puts("different");
}
}

POJ1635 树的最小表示法(判断同构)的更多相关文章

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

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

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

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

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

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

  4. HDU 1954 Subway tree systems (树的最小表示法)

    题意:用一个字符串表示树,0代表向下走,1代表往回走,求两棵树是否同构. 分析:同构的树经过最小表示会转化成两个相等的串. 方法:递归寻找每一棵子树,将根节点相同的子树的字符串按字典序排列,递归回去即 ...

  5. POJ1635 树的最小表示

    /*zoj1990Subway Tree Systems题目大意:初始时站在树的根节点,若朝着远离根的方向走,记录“”,接近根的方向走记录“”.并且树的每一条边只能来回走一次(即向下和返回).一个合法 ...

  6. POJ 1635 Subway tree systems (树的最小表示法)

    题意:一串01序列,从一个点开始,0表示去下一个点,1表示回到上一个点,最后回到起点,遍历这棵树时每条边当且仅当走2次(来回) 给出两串序列,判断是否是同一棵树的不同遍历方式 题解:我们把每一个节点下 ...

  7. POJ 1635 树的最小表示法

    题目大意: 用一堆01字符串表示在树上走动的路径,0表示往前走,1表示往回走,问两种路径方式下形成的树是不是相同的树 我们可以利用递归的方法用hash字符串表示每一棵子树,然后将所有子树按照字典序排序 ...

  8. luogu P5043 【模板】树同构 hash 最小表示法

    LINK:模板 树同构 题目说的很迷 给了一棵有根树 但是重新标号 言外之意还是一棵无根树 然后要求判断是否重构. 由于时无根的 所以一个比较显然的想法暴力枚举根. 然后做树hash或者树的最小表示法 ...

  9. 『Tree nesting 树形状压dp 最小表示法』

    Tree nesting (CF762F) Description 有两个树 S.T,问 S 中有多少个互不相同的连通子图与 T 同构.由于答案 可能会很大,请输出答案模 1000000007 后的值 ...

随机推荐

  1. C# for循环的嵌套 作用域

    for() {   循环体可以套无数个for循环 } 比如:for() { for() { for() {... ...这里面可以镶嵌无数个for循环} } } 也可以这样 for() { for() ...

  2. bunzip2命令

    bunzip2——解压缩.bz2格式文件 命令所在路径:/usr/bin/bunzip2 示例1: # bunzip2 yum.log.bz2 解压当前目录下的yum.log.bz2为yum.log, ...

  3. 粗谈Android未来前景

    Andriod作为智能手机机兴起的操作系统,有着非同寻常的地位.而相对于他的竞争对手ios,两大系统各有自身的优缺点,有太多的不同点,但相比较用户体验来说ios略胜一筹. Android系统极具开发性 ...

  4. WPF知识点全攻略05- XAML内容控件

    此处简单列举出布局控件外,其他常用的控件: Window:WPF窗口 UserControl:用户控件 Page:页 Frame:用来浏览Page页 Border:嵌套控件,提供边框和背景. Butt ...

  5. dinner 后台 nodemon 部署 Koa (关闭everything 安装或排除node_modules) # mysql 没开192.168.x.x 需要设置一下 #Navicat Premium,mysql 数据库版本有要求:mysql-5.7.17.msi 对??的支持

    tip1:新建数据库 记得选 字符集和排序规则 utf8 -- UTF-8 Unicode utf8_general_ci 后台链接部分 1. 全局管理员安装 nodemon,后台热部署(右键 管理员 ...

  6. 安装vc++6.0的步骤

    我们学习计算机,就必须要先将编程的c语言学好,打好基础,学习c语言最好的方法就是多上机联系,对于联系我们需要在自己的电脑上安装vc++6.0来进行平日里的联系.1.打开电脑进行联网,打开浏览器搜索vc ...

  7. OpenCV2:总结篇 core模块

    一.cv::Mat 1.作用 cv::Mat表示图像类,用来操作图像和矩阵,它包含很多属性和方法 2.构造方法 cv::Mat image;  //cv::Mat image()      无参数构造 ...

  8. 第1节 flume:11、flume的failover机制实现高可用

    1.4 高可用Flum-NG配置案例failover 在完成单点的Flume NG搭建后,下面我们搭建一个高可用的Flume NG集群,架构图如下所示: 图中,我们可以看出,Flume的存储可以支持多 ...

  9. react入门(上)

    1. ReactJS是什么? 1). Facebook开源的一个js库 2). 一个用于动态构建用户界面的js库2. React的特点 * Declarative(声明式编码) * Component ...

  10. dubbo---------timeout与retires

    相信很多人都见过这张图,这张图说明了提供者与消费者之间的关系,下面就介绍一下这个图是什么意思. 1.角色解释: Provider: 暴露服务的服务提供者. Consumer: 调用远程服务的服务消费者 ...