Poj 1635 subway tree systems hash method to determine whether the root tree is homogeneous

Source: Internet
Author: User
An example of hash's application in the competition of Informatics

"It is easy to think of an algorithm that uses two strings to represent two shards, But what should we do if we use hash?
You can use a tree-like Recursive Method to Calculate the hash value:

For a node v, first obtain the hash values of all its son nodes and sort them in ascending order as H1, H2, „, HD. Then the hash value of V can be calculated:

(A * P) ^ H1 mod q) * P ^ H2 mod q ).....

In other words, it is from a constant, multiplied by p each time, and an element is exclusive or, then divided by Q to get the remainder, and then multiplied by P, which is different from the next element or, divide by Q and perform the remainder until the last element is reached. At last, multiply the obtained result by B and then obtain the remainder of Q. "

 
#include <cstdio>#include <cstring>#include <iostream>#include <map>#include <set>#include <vector>#include <string>#include <queue>#include <deque>#include <bitset>#include <list>#include <cstdlib>#include <climits>#include <cmath>#include <ctime>#include <algorithm>#include <stack>#include <sstream>#include <numeric>#include <fstream>#include <functional>using namespace std;#define MP make_pair#define PB push_backtypedef long long LL;typedef unsigned long long ULL;typedef vector<int> VI;typedef pair<int,int> pii;const int INF = INT_MAX / 3;const double eps = 1e-8;const LL LINF = 1e17;const double DINF = 1e60;const int maxn = 3005;const int C = 12345, p = 67890, q = 1e9 + 7;char str1[maxn], str2[maxn];VI ch[maxn], hash[maxn];int fa[maxn];int dfs(int now) {    int m = ch[now].size();    if(m == 0) return C;    for(int i = 0;i < m;i++) {        hash[now].PB(dfs(ch[now][i]));    }    sort(hash[now].begin(),hash[now].end());    int ret = C;    for(int i = 0;i < m;i++) {        ret = (ret * p) ^ hash[now][i] % q;    }    return ret;}int gethash(char *str) {    memset(fa,0,sizeof(fa));    int len = strlen(str), u = 0, sz = 1;    for(int i = 0;i < len;i++) {        ch[i].clear(); hash[i].clear();    }    for(int i = 0;i < len;i++) {        if(str[i] == ‘0‘) {            ch[u].PB(sz);            fa[sz] = u;            u = sz++;        }        else {            u = fa[u];        }    }    return dfs(0);}int main() {    int T; scanf("%d",&T);    while(T--) {        scanf("%s%s",str1,str2);        if(gethash(str1) == gethash(str2)) {            puts("same");        }        else puts("different");    }    return 0;}

  

Poj 1635 subway tree systems hash method to determine whether the root tree is homogeneous

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.