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. Git从fork分支开始的过程整理

    文章适用于团队合作的时候多个人向一个repo贡献,整理了Git从fork分支开始的过程. 1. Fork 在github上你要贡献的repo(eg.http://github/remote/test. ...

  2. Java-MyBatis-MyBatis3-XML映射文件:缓存

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:缓存 1.返回顶部 1. 缓存 MyBatis 内置了一个强大的事务性查询缓存机制,它可以非常方便地配置和定制. 为了使它更 ...

  3. 杂项-公司:SAMSUNG

    ylbtech-杂项-公司:SAMSUNG 三星集团是韩国最大的跨国企业集团,同时也是上市企业全球500强,三星集团包括众多的国际下属企业,旗下子公司有:三星电子.三星物产.三星航空.三星人寿保险等, ...

  4. php面向对象的初认识

    面向对象的基本概念 面向对象的三大特征:继承 封装 多态 类和对象: 类是一个抽象的概念 对象是一个具体的实例 张三是一个对象,李四也是一个对象.王五同样是一个对象..... 他们都隶属于“人”这个“ ...

  5. 黑裙辉DS918+安装错误码21,安装教程 重装需要重新制作启动盘

    不然报错误码21    

  6. 06_springmvc之参数绑定(pojo和集合)

    一.包装类型pojo参数绑定 实现方法: 第一种方法:在形参中 添加HttpServletRequest request参数,通过request接收查询条件参数. 第二种方法:在形参中让包装类型的po ...

  7. Python2.7安装matplotlib、numpy

    Windows版本 一.从官网下载python2.7,安装过程中把pip给装上,并且把path也选上,就是把自带的工具全选: 二.在cmd下输入python,会进入交互式界面,出了问题很大程度上是因为 ...

  8. 【论文翻译】NIN层论文中英对照翻译--(Network In Network)

    [论文翻译]NIN层论文中英对照翻译--(Network In Network) [开始时间]2018.09.27 [完成时间]2018.10.03 [论文翻译]NIN层论文中英对照翻译--(Netw ...

  9. Django项目:CMDB(服务器硬件资产自动采集系统)--06--06CMDB测试Linux系统采集硬件数据的命令01

    #base.py # ————————01CMDB获取服务器基本信息———————— from config import settings #配置文件 class BasePlugin(object ...

  10. 【一坨理论AC的题】Orz sxy大佬

    1.UVA10891 Game of Sum 2.LA4254 Processor . 3.UVA10905 Children's Game 4.UVA11389 The Bus Driver Pro ...