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. OPENFIRE 使用Hazelcast插件进行集群

    参考资料:http://www.linuxidc.com/Linux/2014-01/94850.htm   https://www.igniterealtime.org/projects/openf ...

  2. Injection of autowired dependencies failed;错误解决

    代码自动生成的时候可能出现这个问题,反正我是找了半天才发现.serviceimp层不要写抽象类的声明abstract,这个删掉.

  3. shell脚本,awk实现每个数字加1.

    [root@localhost add]# cat file [root@localhost add]# cat file|awk '{for(i=1;i<=NF;i++){$i+=1}}1' ...

  4. Philipp Wagner

    本文大部分来自OpenCV官网上的Face Reconition with OpenCV这节内容(http://docs.opencv.org/modules/contrib/doc/facerec/ ...

  5. 如何禁用Visual Studio的Browser Link功能

    在Web.Config的AppSetting节点添加<add key="vs:EnableBrowserLink" value="false"/>

  6. (4)zabbix监控第一台服务器

    2. zabbix监控服务器 创建主机,选择模板以及录入基本信息,过一分钟左右,就可以看到cpu.内存.硬盘等等使用情况.本节以图文为主.by the way, zabbix中文翻译很烂,config ...

  7. nginx 无法加载css/js图片等文件 404 not fund

    刚配置Nginx反向代理,Nginx可能会出现无法加载css.js或者图片等文件,这里需要在配置文件*.conf里面加上如下配置项. location ~ .*\.(js|css|png|jpg)$ ...

  8. tkinter学习-文本框

    阅读目录 Entry 输入框 Text 文本框 Entry: 说明:输入控件,用于显示简单的文本内容 属性:在输入框中用代码添加和删除内容,同样也是用insert()和delete()方法 from ...

  9. 《零基础入门学习Python》【第一版】视频课后答案第005讲

    测试题答案: 0.Python中: int:整型 bool:布尔类型 float:浮点型 str:字符串类型 1.为什么布尔类型(bool)的TRUE和FALSE分别用0和1表示? 计算机只认识二进制 ...

  10. Poj 1041--欧拉回路

    Description Little Johnny has got a new car. He decided to drive around the town to visit his friend ...