树的最小表示法
给定两个有根树的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.
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.
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