POJ1635Subway tree systems

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

Northwestern Europe 2003

【题解】

树的括号序列最小表示法,0相当于‘(‘,1相当于‘)‘https://www.byvoid.com/zhs/blog/directed-tree-bracket-sequence

直接在序列上递归到叶子,然后从叶子向上合并,交换子树位置,小的子树在前面

我破例用了死活不想用的又慢又不自由的string。

STL用起来真是爽啊

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <algorithm>
 8 std::string s1, s2;
 9 int t;
10 std::string dfs(std::string now)
11 {
12     std::vector<std::string> s;
13     std::string re = "";
14     int flag = 0, pre = 1;
15     for(register int i = 0;i < now.size();++ i)
16     {
17         if(now[i] == ‘0‘)++ flag;
18         else -- flag;
19         if(flag == 0)
20         {
21             std::string tmp = dfs(now.substr(pre, i - pre));
22             if(tmp == "")s.push_back("01");
23             else s.push_back(‘0‘ + tmp + ‘1‘);
24             pre = i + 2;
25         }
26     }
27     std::sort(s.begin(), s.end());
28     for(register int i = 0;i < s.size();++ i)re += s[i];
29     return re;
30 }
31 int main()
32 {
33     scanf("%d", &t);
34     for(;t;-- t)
35     {
36         std::cin >> s1 >> s2;
37         if(dfs(s1) == dfs(s2))printf("same\n");
38         else printf("different\n");
39     }
40     return 0;
41 } 

POJ1635

时间: 2024-10-13 01:09:06

POJ1635Subway tree systems的相关文章

【POJ】【1635】Subway Tree Systems

树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同构的,思路是用树的最小表示法去做.这里用的最小表示法就是将树的所有子树分别用1个字符串表示,要按字典序排序将他们依依连接起来.连接后如果两个字符串是一模一样的,那么他们必然是同构的.这样原问题就变成了子问题,子树又是一颗新的树. 1 Source Code 2 Problem: 1635 User:

[有向树的最小表示] poj 1635 Subway tree systems

题目链接: http://poj.org/problem?id=1635 Subway tree systems Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6541   Accepted: 2747 Description Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, th

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

Subway tree systems POJ - 1635 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树 /* 在poj上交需要加一个string头文件,不然会CE 树的最小表示 这里用的最小表示法就是将树的所有子树分别用1个字符串表示,要按字典序排序将他们依依连接起来.连接后如果两个字符串是一模一样的,那么他们必然是同构的.这样原问题就变成了子问题,子树又是一颗新的树. */ #include<iostream>

poj-1635 Subway tree systems(推断两个有根树是否同构)-哈希法

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 o

[ POJ ][ HASH ] 1635 Subway tree systems

首先,对于一个树,我们可以有一种压缩表示: 0010011101001011 其中 0 表示向下走,1 表示向上走.于是: 00100111|01|001011 对于每一段的 0 1 出现次数相同,这种hash方法叫 树的最小表示法 . 1635 题目精简大意:给你n对01字符串,判断每一对儿表示的是不是同一个树,方法: 1.定义 cnt, start, end 来记录当前0 1之和是否相等,start,end 记录相等时所得字数的范围. 2.去首尾得到子串,递归进行1步骤直到子串只为“0 1”

POJ 1635 Subway tree systems Hash法判断有根树是否同构

Hash在信息学竞赛中的一类应用 中的某道例题 "不难想到的算法是使用两个字符串分别表示两棵树,但是如果使用Hash的话应该怎么做呢? 可以使用一种类似树状递推的方法来计算Hash值:  对于一个节点v,先求出它所有儿子节点的Hash值,并从小到大排序,记作H1,H2,„,HD.那么v的Hash值就可以计算为:   (((a * p) ^ H1 mod q) * p ^ H2 mod q).....  换句话说,就是从某个常数开始,每次乘以p,和一个元素异或,再除以q取余,再乘以p,和下一个元素

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

poj图论解题报告索引

最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman-ford,spfa) poj1511 - Invitation Cards(单源来回最短路径,spfa邻接表) poj1797 - Heavy Transportation(最大边,最短路变形,dijkstra,spfa,bellman-ford) poj2240 - Arbitrage(汇率问题,

DP题目列表/弟屁专题

声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 195