题目链接:http://poj.org/problem?id=1635
题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯。
我刚开始自己设计了哈希函数,不知道为什么有问题。。。。参考了http://www.cnblogs.com/jackiesteed/articles/2065307.html,他的就是对的。。我也不知道为什么。。
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <vector> 5 #include <queue> 6 #include <string> 7 #include <set> 8 #include <ctime> 9 #include <cstdlib> 10 using namespace std; 11 typedef long long LL; 12 typedef pair<int,int> PII; 13 typedef vector<int> vec; 14 typedef vector<vec> mat; 15 #define AA first 16 #define BB second 17 #define PB push_back 18 19 int T,h[11000]; 20 char a[3300],b[3300]; 21 char *p; 22 23 void init(){ 24 srand(time(NULL)); 25 for(int i=0;i<11000;i++){ 26 h[i] = rand()%19001; 27 } 28 } 29 30 int calc_hash(int i){ 31 int res = h[5000+i]; 32 while(*p&&*p++=='0'){ 33 res = (res + h[i]*calc_hash(i+1))%19001; 34 } 35 return (res*res)%19001; 36 } 37 38 int main(){ 39 init(); 40 scanf("%d",&T); 41 while( T-- ){ 42 scanf("%s%s",a,b); 43 p = a; 44 int ha = calc_hash(0); 45 p = b; 46 int hb = calc_hash(0); 47 if( ha==hb ) puts("same"); 48 else puts("different"); 49 } 50 return 0; 51 }
浙公网安备 33010602011771号