有根树同构问题有两种解法:
1. 树的最小表示,用一系列括号表示一棵树,一对括号内是一颗子树,由于子树没有次序,因此可以将树的所有子树的最小表示(树的递归结构)按照字典序排序,即为树的最小表示,比较两颗树的最小表示,相同则同构。详细可参考:www.byvoid.com/blog/directed-tree-bracket-sequence/
算法的复杂度应该是O(n^2lgn)
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- // MinRep is DFS, return the min representation of a tree.
- // Note: argument tree must be const reference!
- // Because the passed rvalue is c_str "" or anonymous string obj, they are
- // const!
- // use c_str and pass pointer is more faster!
- string MinRep(const string &tree) {
- if (!tree.size())
- return "";
- int tn = 0;
- vector<string> subtrees;
- for (int i = 0, j = 0, cnt = 0; j < tree.size(); ++j) {
- if (tree[j] == '0') ++cnt;
- else --cnt;
- if (!cnt) {
- ++tn;
- string tmp1 = "", tmp2 = tree.substr(i + 1, j - i - 1);
- subtrees.push_back("0" + (j - i == 1 ? MinRep(tmp1) : MinRep(tmp2)) + "1");
- //subtrees.push_back("0" + (j - i == 1 ? MinRep("") : MinRep(tree.substr(i + 1, j - i - 1))) + "1");
- i = j + 1;
- }
- }
- sort(subtrees.begin(), subtrees.end());
- string min_rep;
- for (int i = 0; i < tn; ++i)
- min_rep += subtrees[i];
- return min_rep;
- }
- int main() {
- int t;
- cin >> t;
- while (t--) {
- string tree1, tree2;
- cin >> tree1 >> tree2;
- string min_rep1 = MinRep(tree1), min_rep2 = MinRep(tree2);
- if (min_rep1 == min_rep2)
- cout << "same" << endl;
- else
- cout << "different" << endl;
- }
- return 0;
- }
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。
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- #define mod 9997
- char str1[10000];
- char str2[10000];
- int h[10000],len,cur;
- char *p;
- int hashing(int j)
- {
- int sum=h[j];
- while(*p!='\0'&&*p++=='0') //每次检查是否1时还加1,使回溯时跳出循环
- {
- sum=(sum+hashing(j+1)*h[j])%mod;
- }
- return (sum*sum)%mod;
- }
- int main ()
- {
- for(int i=0;i<10000;++i)
- h[i]=rand()%mod;
- int test;scanf("%d",&test);
- while(test--)
- {
- scanf("%s%s",str1,str2);
- if(strlen(str1)!=strlen(str2))
- {
- printf("different\n");
- continue;
- }
- len=strlen(str1);
- p=str1;
- int a=hashing(1);
- p=str2;
- int b=hashing(1);// 多次hash 可以避免冲突,提高正确率
- if(a==b)
- printf("same\n");
- else printf("different\n");
- }
- system("pause");
- return 0;
- }