树的最小表示法


  给定两个有根树的dfs序,问这两棵树是否同构

  题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html

题目要求判断两棵树是否是同构的,思路是用树的最小表示法去做。这里用的最小表示法就是将树的所有子树分别用1个字符串表示,要按字典序排序将他们依依连接起来。连接后如果两个字符串是一模一样的,那么他们必然是同构的。这样原问题就变成了子问题,子树又是一颗新的树。

 Source Code
Problem: User: sdfzyhy
Memory: 1160K Time: 672MS
Language: G++ Result: Accepted Source Code //PKUSC 2013 R1 C
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
using namespace std;
typedef long long LL;
inline int getint(){
int r=,v=; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-;
for(; isdigit(ch);ch=getchar()) v=v*-''+ch;
return r*v;
}
const int N=;
/*******************template********************/
string s1,s2; string dfs(string s){
vector<string>a;
string ans="";
int t=,st=;
rep(i,s.length()){
if (s[i]=='') t++;
else t--;
if (t==){
if (i- > st+){
a.pb(""+dfs(s.substr(st+,i--st))+"");
}else a.pb("");
st=i+;
}
}
sort(a.begin(),a.end());
rep(i,a.size()) ans=ans+a[i];
return ans;
} int main(){
#ifndef ONLINE_JUDGE
freopen("C.in","r",stdin);
freopen("C.out","w",stdout);
#endif
int T=getint();
while(T--){
cin >> s1 >> s2;
if (dfs(s1)==dfs(s2)) puts("same");
else puts("different");
}
return ;
}
Subway tree systems
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7060   Accepted: 2935

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

[Submit]   [Go Back]   [Status]   [Discuss]

【POJ】【1635】Subway Tree Systems的更多相关文章

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

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

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

  3. 【树哈希】poj1635 Subway tree systems

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

  4. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  5. 【POJ 1459 power network】

    不可以理解的是,测评站上的0ms是怎么搞出来的. 这一题在建立超级源点和超级汇点后就变得温和可爱了.其实它本身就温和可爱.对比了能够找到的题解: (1)艾德蒙·卡普算法(2)迪尼克算法(3)改进版艾德 ...

  6. 【POJ 2728 Desert King】

    Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...

  7. 【POJ 2976 Dropping tests】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 13849Accepted: 4851 Description In a certa ...

  8. 【POJ 3080 Blue Jeans】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...

  9. 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)

    1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ...

随机推荐

  1. maraidb半同步复制

    半同步复制是在主从同步复制的基础之上,主从同步前面我的博文有介绍 Semisynchronous Replication(半同步复制)则一定程度上保证提交的事务已经传给了至少一个备库 MySQL在加载 ...

  2. HTML5 学习笔记 1

    1.音频.视频 <!DOCTYPE HTML> <html> <body> <audio controls="controls"> ...

  3. FileOutputSream

    package cd.itcast.fileinputstream; import java.io.File; import java.io.FileNotFoundException; import ...

  4. Learning Scrapy笔记(七)- Scrapy根据Excel文件运行多个爬虫

    摘要:根据Excel文件配置运行多个爬虫 很多时候,我们都需要为每一个单独的网站编写一个爬虫,但有一些情况是你要爬取的几个网站的唯一不同之处在于Xpath表达式不同,此时要分别为每一个网站编写一个爬虫 ...

  5. STL之容器基本操作

    容器类 STL Container Header Applications vector <vector> 直接访问任意元素,快速插入.删除尾部元素 deque <deque> ...

  6. linux kernel 0.11 bootsect

    bootsect作用 ①将自己移动到0x90000处 ②将setup从磁盘读到0x90200处 ③将system从磁盘读到0x10000处 寄存器 汇编代码中存在:数据段data seg 栈段 sta ...

  7. 微信支付开发,再次签名,APP调用

    1.商户服务器生成支付订单,先调用[统一下单API]生成预付单,获取到prepay_id后将参数再次签名传输给APP发起支付. 再次生成签名的时候,按照接口: https://pay.weixin.q ...

  8. discuz分类信息地区联动菜单字段

    1 = 河南省 1.1 = 郑州市 1.1.1 = 中原区 1.1.2 = 二七区 1.1.3 = 管城区 1.1.4 = 金水区 1.1.5 = 上街区 1.1.6 = 惠济区 1.1.7 = 巩义 ...

  9. Extjs 下拉框下拉选项为Object object

    使用Extjs的下拉框出现下拉选项为Object object的问题. 原因在于对store属性提供的是data信息,而不是store对象

  10. windows+nginx+fcgi配置

    最近项目要求要学习一下nginx的知识,由于自己学疏才浅,搞了一天多终于基本搭建出来了,怕日后忘记,所以在此记录一下 nginx的历史,应用和种种就不记录了,自行百度.....Fcgi 相比cgi的好 ...