您的位置:首页 > 其它

POJ 1635 Subway tree systems(HASH+判断两棵有根树是否同构)

2015-10-22 18:02 447 查看
题意:给出两棵树,判定是否同构(通过旋转使得形状相同)。

思路:HASH+dfs。HASH的方法用的就是杨弋《Hash在信息学竞赛中的一类应用》里的方法,但是一开始hash的p选的不好导致wa了,换了一个素数就ac了......真是看脸.......

#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>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
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;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: