【POJ】【1635】Subway Tree Systems
树的最小表示法
给定两个有根树的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 ;
}
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 7060 | Accepted: 2935 | 
Description
 
Input
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
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的更多相关文章
- poj 1635 Subway tree systems(树的最小表示)
		Subway tree systems POJ - 1635 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树 /* 在po ... 
- 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 ... 
- 【树哈希】poj1635 Subway tree systems
		题意:给你两颗有根树,判定是否同构. 用了<Hash在信息学竞赛中的一类应用>中的哈希函数. len就是某结点的子树大小,g是某结点的孩子数+1. 这个值也是可以动态转移的!具体见论文,所 ... 
- 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)
		Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ... 
- 【POJ 1459 power network】
		不可以理解的是,测评站上的0ms是怎么搞出来的. 这一题在建立超级源点和超级汇点后就变得温和可爱了.其实它本身就温和可爱.对比了能够找到的题解: (1)艾德蒙·卡普算法(2)迪尼克算法(3)改进版艾德 ... 
- 【POJ 2728 Desert King】
		Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ... 
- 【POJ 2976 Dropping tests】
		Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 13849Accepted: 4851 Description In a certa ... 
- 【POJ 3080 Blue Jeans】
		Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ... 
- 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)
		1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ... 
随机推荐
- float:left居中对齐
			<div class="M1180"><div class="services"> <div class="serv_c ... 
- Hadoop在win7下部署的问题
			问题: 为了测试方便所以在win7下部署了伪分布式hadoop运行环境,但是部署结束后在命令行运行hadoop命令创建一个用户文件目录时出现了一下情况: 系统找不到指定的批标签- make_comma ... 
- wordpress学习-themes-001
			这一篇主要是来记录wordpress theme的内容.关于为什么要自己编写wordpress theme的理由,相信大家都有各自的体会.想让自己的博客变的更加突出?更加个性话?wordpress t ... 
- (转)Android系统自带Activity样式(@android:style/)
			在AndroidManifest.xml文件的activity中配置 1.android:theme="@android:style/Theme" 默认状态,即如果theme这里不 ... 
- 国际制造执行系统(MES)应用与发展
			某些专家认为,当今制造业的生存三要素是信息技术(IT).供应链管理(SCM)和成批制造技术.使用信息技术就是由依赖人工的作业方式转变为作业的快速化.高效化,大量减少人工介入,降低生产经营成本:供应链管 ... 
- 第三节:卸载AppDomain
			AppDomain很出色的一个能力就是它允许卸载.卸载AppDomain会导致CLR卸载AppDomain中的所有程序集.还会释放AppDomain的Loader堆.为了卸载一个AppDomain,可 ... 
- 关于iphone消息推送把C#当服务器端来发送
			看了苹果消息推送文档,感觉推送很简单的,但是还是按个人习惯把这些简单知识记录下来,在需要时候再查看一下! 在开发之前,要准备以下的资料 1.证书(包括产生证书和调试证书) 2.证书密码 3.唯一标识( ... 
- StyleCop学习笔记——自定义规则
			本文将简单的一步一步的指导这可能有助于学习如何创建自己的规则 1.创建一个项目. Visual Studio创建一个新的类库项目.NET3.5 2.引用两个DLL,StyleCop.dll和Style ... 
- EXCLE使用宏生成目录
			宏代码: Sub mu() Dim i As Integer Dim ShtCount As Integer Dim SelectionCell As Range ShtCount = Workshe ... 
- servlet中的cookie
			cookie的机制是:从客户端(浏览器)发送请求到服务器,然后服务器把接受的信息回写到客户端,这个信息在客户端跟服务器之间进行交互. 下面是一个创建cookie的小案例 //如何创建cookie pa ... 
