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. sql service 查询分析数据库

    --学会通配符 https://blog.csdn.net/blackfwhite/article/details/80382849 --学会变量中的变量 https://www.cnblogs.co ...

  2. x-shell配置远程连接

    1. 打开x-shell 2. 配置编译属性 3. 配置用户的身份证信息 5. 配置完成之后选择连接

  3. Luogu P4463 [国家集训队] calc

    WJMZBMR的题果然放在几年后看来仍然挺神,提出了一种独特的优化DP的方式 首先我们想一个暴力DP,先定下所有数的顺序(比如强制它递增),然后最后乘上\(n!\)种排列方式就是答案了 那么我们容易想 ...

  4. sql server 处理分母为空

    SP 前面加下面设置,会忽略错误结果 直接返回null 不会导致SP 失败 SET ANSI_WARNINGS OFFSET ARITHABORT OFFSET ARITHIGNORE ON

  5. WINDOWS-API:取得系统语言种类-GetOEMCP

    GetOEMCP VB声明 Declare Function GetOEMCP Lib "kernel32" Alias "GetOEMCP" () As Lo ...

  6. python3.x中的33个保留字

    Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type " ...

  7. 项目之socket

    客户端socket 客户端套接字完成的任务很统一,发送请求,接收请求结果 可以封装成一个方法 使用的tcp协议存在粘包问题,故需要自定义报头 import json import struct #项目 ...

  8. python爬虫入门三:requests库

    urllib库在很多时候都比较繁琐,比如处理Cookies.因此,我们选择学习另一个更为简单易用的HTTP库:Requests. requests官方文档 1. 什么是Requests Request ...

  9. Linux磁盘与文件系统管理 之 认识EXT2系统

    1 磁盘组成与分区 1.1 磁盘物理组成 (1)圆形盘片-记录数据 (2)机械手臂及磁头-读写盘片数据 (3)主轴马达-使得机械手臂成功读写数据驱动 1.2 盘片物理组成 (1)扇区-最小物理存储单位 ...

  10. int long long 的范围

    unsigned   int     0-4294967295   (10位数,4e9) int                        -2147483648-2147483647  (10位 ...