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. We'll be fine.

    可怜的人心中都有一种信念,那就是 明天会更好. Everything will be fine. 我拥见风来,嗅见花开,是避不掉的厉寒,是止不住的徘徊.

  2. Spring MVC(四)--控制器接受pojo参数

    以pojo的方式传递参数适用于参数较多的情况,或者是传递对象的这种情况,比如要创建一个用户,用户有十多个属性,此时就可以通过用户的pojo对象来传参数,需要注意的是前端各字段的名称和pojo对应的属性 ...

  3. Java - 关于覆盖和重写的总结

    公众号偶然看到的一个帖子,构造方法,类方法,final方法,哪些能覆盖,哪些能重载,初学时也是被这些术语搞的很迷糊 现在有时间了对这些做一个总结.全是自己的语言,可能不是很全面,表达意思应该够清楚 一 ...

  4. python学习笔记4_数据清洗与准备

    一.处理缺失值 pandas使用浮点值NaN(Not a Number)来显示缺失值,并将缺失值称为NA(not available(不可用)). NA常用处理方法: dropna:根据每个标签的值是 ...

  5. Java程序员面试题收集(1)

    一.Java基础部分 1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: 1)抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪 ...

  6. nulls_hlist原理 和 tcp连接查找

    原文链接 http://abcdxyzk.github.io/blog/2018/09/28/kernel-sk_lookup/

  7. 12.Hibernate多对多关系

    JavaBean的编写 Person private long pid ; private String name ; private Set<Role> roles = new Hash ...

  8. 03.Hibernate配置文件之核心配置文件

    一.核心配置文件的两种配置方式 1.属性文件方式 hibernate.properties(基本不会选用 hibernate.connection.driver_class=com.mysql.jdb ...

  9. Nginx 的常用命令

    nginx命令,先来一波基本操作: start nginx // 启动Nginx nginx -t // 测试配置文件 nginx -v // 查看Nginx版本 nginx -V // 查看Ngin ...

  10. 19-10-15-W

    暴力终于不跪了$\text{QvQ}$ z总j结 考试开始看到几个大字:Day1 Happy-(××终于不用爆〇了哈哈哈哈!!) 开T1.一看,不是在线仙人球嵌套动态网络路径剖分优化的分支定界贪心剪枝 ...