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;
}
思路: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;
}
相关文章推荐
- 1075 - Incorrect table definition;there can be only one auto column and it must be defined as a key
- IOS-常用的NSDate方法
- [.Net码农]RDL和RDLC使用ReportViewer显示报表时筛选导出项
- 【笔记】Android中Toast的使用技巧
- 使用重置按钮,重置表单信息
- (高级篇 Netty多协议开发和应用)第十章-Http协议开发应用(基于Netty的HttpServer和HttpClient的简单实现)
- SQL Server中删除以指定字符开头的存储过程
- android:强大的图片下载和缓存库Picasso
- 基于zookeeper的分布式Queue
- oracle XE精简版不支持FETCH FIRST 5 ROWS ONLY语句
- base64加解密
- [.Net码农]RDLC打印控件失败
- 状态为恢复中的SQLServer 数据修复方法
- 使用提交按钮,提交数据
- The Second Blog From Ubuntu Blogilo
- 十大好用的Linux实用工具推荐
- vs编译项目报错:The OutputPath property is not set for this project
- [.Net码农]RDLC报表打印时提示 客户端无法加载打印控件的问题
- dbUtils结果集处理器
- typedef使用