# POJ 1635 Subway tree systems（HASH+判断两棵有根树是否同构）

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii pair<int, int>
using namespace std;

const int MAXN = 3100;
const int MOD = 19001;
const int P = 131;
const int A = 97;
char t1[MAXN], t2[MAXN];
int n;
int cal(char* T, int st, int ed) {
int cnt = 0, pos = st+1, ans = A;
vector<int> val;
for(int i = st; i <= ed; i++) {
if(T[i] == '0') cnt++;
else cnt--;
if(!cnt) val.push_back(cal(T, pos, i-1)), pos = i+2;
}

sort(val.begin(), val.end());
for(int i = 0; i < val.size(); i++) {
ans *= P;
ans ^= val[i];
ans %= MOD;
}
return ans;
}
int main() {
//freopen("input.txt", "r", stdin);
int T; cin >> T;
while(T--) {
scanf("%s%s", t1, t2);

int ha1 = cal(t1, 0, strlen(t1)-1), ha2 = cal(t2, 0, strlen(t2)-1);
//cout << ha1 << " " << ha2 << endl;
if(ha1 == ha2) puts("same");
else puts("different");
}
return 0;
}



﻿﻿

#### 同构树的判别

2015-11-09 21:37:23

#### poj-1635 Subway tree systems（判断两个有根树是否同构）-哈希法

2014-04-18 09:01:21

#### 7-3 树的同构（25 分）

2017-10-18 16:01:41

#### 判断树的同构

2016-08-09 10:11:54

#### 判断二叉树是否同构

2016-08-11 21:22:07

#### JSOI2016 独特的树叶 树的Hash判同构

2016-05-31 22:47:46

#### [POJ1635]Subway tree systems 判断有根树的同构 有根树哈希

2017-03-21 11:51:01

#### BZOJ4337: [BJOI2015] 树的同构（Hash）

2017-10-06 16:17:56

#### 树的同构

2016-09-17 17:08:33

#### 树的同构--poj 1635（有根树）ustc 1117 （无根树）

2012-10-04 21:37:34