Subway tree systems
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8049   Accepted: 3357

Description

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

Source

 
【题解】
树的括号序列最小表示法,0相当于'(',1相当于')'https://www.byvoid.com/zhs/blog/directed-tree-bracket-sequence
直接在序列上递归到叶子,然后从叶子向上合并,交换子树位置,小的子树在前面
我破例用了死活不想用的又慢又不自由的string。
STL用起来真是爽啊
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
std::string s1, s2;
int t;
std::string dfs(std::string now)
{
std::vector<std::string> s;
std::string re = "";
int flag = , pre = ;
for(register int i = ;i < now.size();++ i)
{
if(now[i] == '')++ flag;
else -- flag;
if(flag == )
{
std::string tmp = dfs(now.substr(pre, i - pre));
if(tmp == "")s.push_back("");
else s.push_back('' + tmp + '');
pre = i + ;
}
}
std::sort(s.begin(), s.end());
for(register int i = ;i < s.size();++ i)re += s[i];
return re;
}
int main()
{
scanf("%d", &t);
for(;t;-- t)
{
std::cin >> s1 >> s2;
if(dfs(s1) == dfs(s2))printf("same\n");
else printf("different\n");
}
return ;
}

POJ1635

 

POJ1635Subway tree systems的更多相关文章

  1. 【POJ】【1635】Subway Tree Systems

    树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...

  2. poj 1635 Subway tree systems(树的最小表示)

    Subway tree systems POJ - 1635 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树 /* 在po ...

  3. 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 ...

  4. [POJ 1635] Subway tree systems (树哈希)

    题目链接:http://poj.org/problem?id=1635 题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯. 我刚开始自己设计了哈希函数,不知道为什么有问题.. ...

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

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

  6. POJ1635:Subway tree systems

    链接:http://poj.org/problem?id=1635 填坑树同构 题目给出的是除根外的括号序列表示. 其实只要跟你说hash大家都能写得出来…… hash函数取个效果别太差的就行了吧 # ...

  7. 【树哈希】poj1635 Subway tree systems

    题意:给你两颗有根树,判定是否同构. 用了<Hash在信息学竞赛中的一类应用>中的哈希函数. len就是某结点的子树大小,g是某结点的孩子数+1. 这个值也是可以动态转移的!具体见论文,所 ...

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

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

  9. POJ 1635 Subway tree systems 有根树的同构

    POJ 1635 题目很简单 给个3000节点以内的根确定的树 判断是否同构.用Hash解决,类似图的同构,不过效率更高. #include<iostream> #include<c ...

随机推荐

  1. iOS之CGcontext.h方法和属性简介

    /* CoreGraphics - CGContext.h Copyright (c) 2000-2012 Apple Inc. All rights reserved. */ #ifndef CGC ...

  2. Linux 静态IP配置

    静态配置文件# vim /etc/sysconfig/network-scripts/ifcfg-不同系统不一样主要几个配置TYPE=EthernetBOOTPROTO=static/noneNAME ...

  3. Python学习day14-函数进阶(2)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. [Swoole系列入门教程 5] UDP协议和demo

    • 客户端服务端没有任何联系 • 指定地址跟端口,不关心消息是否发送成功 • 心跳检测不能影响到客户端• udp建立长连接

  5. leetcode 324 Wiggle Sort 2

    利用中位数的概念,中位数就是将一组数分成2等份(若为奇数,则中位数既不属于左也不属于右,所以是2等份),其一组数中任何一个元素都大于等于另一组数 那么我们是不是只要一左一右配合着插入,就保证了差值+- ...

  6. redhat linux卸载自带的Java1.4.2安装JDK6

    一.卸载jdk1.4 由于Redhat Enterprise Linux 5.6 中自带安装了jdk1.4.2的,所以在安装jdk1.6前我把jdk1.4.2的卸了,步骤如下: 1.打开终端输入 yu ...

  7. 关于python的列表操作(一):取值,增加,修改,删除

    # 列表操作 name_list = ["wang", "niu", "bai", "sui"] # 取值 print( ...

  8. python函数当容器

    def func(): pass func2 = func func2() i = [func,func2] for a in i: a() 函数名就是内存地址,加()代表执行

  9. struts2-自定义拦截器-struts2标签

    1 自定义拦截器 1.1 架构 1.2 拦截器创建 创建方式1 创建方式2 创建方式3 ★★★★ 1.3 拦截器api 放行 前后处理 不放行,直接跳转到一个结果页面 不执行后续的拦截器以及Actio ...

  10. Device eth0 does not seem to be present, delaying initialization(VM虚拟机restart service出现此错误)

    >从vmware workstation中克隆(clone)了一个redhat6.0的虚拟机,启动之后发现网卡没有启动.于是重启一下network服务,发现提示错误信息“Device eth0 ...