poj_1635

有根树同构问题有两种解法:


1. 树的最小表示,用一系列括号表示一棵树,一对括号内是一颗子树,由于子树没有次序,因此可以将树的所有子树的最小表示(树的递归结构)按照字典序排序,即为树的最小表示,比较两颗树的最小表示,相同则同构。详细可参考:www.byvoid.com/blog/directed-tree-bracket-sequence/

算法的复杂度应该是O(n^2lgn)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. #include <vector>  
  4. #include <algorithm>  
  5.   
  6. using namespace std;  
  7.   
  8. // MinRep is DFS, return the min representation of a tree.  
  9. // Note: argument tree must be const reference!  
  10. // Because the passed rvalue is c_str "" or anonymous string obj, they are  
  11. // const!  
  12. // use c_str and pass pointer is more faster!  
  13. string MinRep(const string &tree) {  
  14.   if (!tree.size())  
  15.     return "";  
  16.   int tn = 0;  
  17.   vector<string> subtrees;  
  18.   for (int i = 0, j = 0, cnt = 0; j < tree.size(); ++j) {  
  19.     if (tree[j] == '0') ++cnt;  
  20.     else --cnt;  
  21.     if (!cnt) {  
  22.       ++tn;  
  23.       string tmp1 = "", tmp2 = tree.substr(i + 1, j - i - 1);  
  24.       subtrees.push_back("0" + (j - i == 1 ? MinRep(tmp1) : MinRep(tmp2)) + "1");  
  25.       //subtrees.push_back("0" + (j - i == 1 ? MinRep("") : MinRep(tree.substr(i + 1, j - i - 1))) + "1");  
  26.       i = j + 1;  
  27.     }  
  28.   }  
  29.     sort(subtrees.begin(), subtrees.end());  
  30.     string min_rep;  
  31.     for (int i = 0; i < tn; ++i)  
  32.       min_rep += subtrees[i];  
  33.     return min_rep;  
  34. }  
  35.   
  36. int main() {  
  37.   int t;  
  38.   cin >> t;  
  39.   while (t--) {  
  40.     string tree1, tree2;  
  41.     cin >> tree1 >> tree2;  
  42.     string min_rep1 = MinRep(tree1), min_rep2 = MinRep(tree2);  
  43.     if (min_rep1 == min_rep2)  
  44.       cout << "same" << endl;  
  45.     else  
  46.       cout << "different" << endl;  
  47.   }  
  48.   return 0;  
  49. }  

2. hash方法,oi国家集训队论文《Hash在信息学竞赛中的一类应用》有很好的讲解。

一个好的Hash函数最显而易见的特征是,能使不相同的东西经过Hash之后只有很小的几率相同,这样能避免过多冲突的产生。设计合理的hash函数可以用于判重或判等价,例如MD5算法。

对于一个节点v,先求出它所有儿子节点的Hash值,并从小到大排序(子树的顺序不会导致树不同),记作H1,H2,…,HD。那么v的Hash值就可以计算为:

            


时间复杂度为O(nlgn),本质其实和最小表示法一样,但是由于排序用的hash值而不是字符串,使得复杂度降低。 除了这个hash函数意外,其他的合理的hash函数也是可以的(hash是概率方法,冲突率越低越好)。

在网上有一些0ms的算法,很多虽然能ac但是其实并不正确,只是这个题的数据比较弱而已,给出一个可以容易出错的case:

            



上面的hash函数所有叶子节点的hash值相同,网上还找到一种hash函数的设计,不同深度的子树权值不同,hash函数设计的也非常好,感觉有点类似常用的hash表函数,参考:blog.csdn.net/jackyguo1992/article/details/8040300.

代码中,hash函数递归的改变全局指针变量的值,挺巧妙的,而且返回(sum * sum) % mod是必须的,这样才能打破子树相加的线性关系,避免上面提到的容易出错的case。

        

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <cstdlib>  
  3. #include <cstring>  
  4. #include <cstdio>  
  5. #include <cmath>  
  6. using namespace std;  
  7. #define mod 9997  
  8. char str1[10000];  
  9. char str2[10000];  
  10. int h[10000],len,cur;  
  11. char *p;  
  12.   
  13. int hashing(int j)  
  14. {  
  15.     int sum=h[j];  
  16.     while(*p!='\0'&&*p++=='0'//每次检查是否1时还加1,使回溯时跳出循环  
  17.     {  
  18.         sum=(sum+hashing(j+1)*h[j])%mod;  
  19.     }  
  20.     return (sum*sum)%mod;  
  21. }  
  22.   
  23. int main ()  
  24. {  
  25.     for(int i=0;i<10000;++i)  
  26.         h[i]=rand()%mod;  
  27.     int test;scanf("%d",&test);  
  28.     while(test--)  
  29.     {  
  30.         scanf("%s%s",str1,str2);  
  31.         if(strlen(str1)!=strlen(str2))  
  32.         {  
  33.             printf("different\n");  
  34.             continue;  
  35.         }  
  36.         len=strlen(str1);  
  37.         p=str1;  
  38.         int a=hashing(1);  
  39.         p=str2;  
  40.         int b=hashing(1);// 多次hash 可以避免冲突,提高正确率  
  41.         if(a==b)  
  42.             printf("same\n");  
  43.         else printf("different\n");  
  44.     }  
  45.     system("pause");  
  46.     return 0;  
  47. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值