树的同构判定的两种方法
树的同构判定
hash方法跟普通方法真的差很多……把两种都贴出来看下
- #include <stdio.h>
- #include<stdlib.h>
- int h[9999];
- char s[9999],t[9999],*p;
- int hash(int j)
- {
- int sum=h[j+5000];
- while (*p && *p++=='0')
- sum=(sum+h[j]*hash(j+1)) % 19001;
- return ((sum*sum) % 19001);
- }
- int main()
- {
- int i,n;
- scanf("%d",&n);
- for(i=0;i<9999;i++)
- h[i]=rand()%19001;
- while (n--)
- {
- scanf("%s%s",s,t);
- p=s;
- i=hash(0);
- p=t;
- printf("%sn",i==hash(0)?"same":"different");
- //可以多次hash,避免冲突
- }
- return 0;
- }
普通方法
- #include <iostream>
- #include <algorithm>
- #include <stdlib.h>
- using namespace std;
- struct node
- {
- bool visited;
- int no;
- node* first;
- node* next;
- };
- char w1[3010],w2[3010];
- inline void init(node* x)
- {
- x->visited=0;x->no=0;
- x->first=NULL;x->next=NULL;
- }
- inline void creat(node*& x,char *str,int& s)
- {
- if(str[s]=='') return ;
- if(str[s]=='0')
- {
- x=new node;
- init(x);
- x->no=s;
- s++;
- creat(x->first,str,s);
- node *p=x;
- while(p->next) p=p->next;
- creat(x->next,str,s);
- }
- else s++;
- }
- inline int cal(node *x)
- {
- int sum=0;
- if(x==NULL) return 0;
- node *p=x->first;
- while(p)
- {
- sum+=cal(p);
- p=p->next;
- }
- x->no=sum+1;
- x->visited=0;
- return x->no;
- }
- inline void setvisit(node *p,bool tag)
- {
- if(p==NULL) return ;
- node *q=p->first;
- while(q)
- {
- setvisit(q,tag);
- q=q->next;
- }
- p->visited=tag;
- }
- inline void tsort(node *x)
- {
- if(x==NULL || x->no==1)
- return ;
- node *p=x->first;
- while(p)
- {
- tsort(p);
- p=p->next;
- }
- node *q=x->first->next;
- x->first->next=NULL;
- while (q!=NULL)
- {
- node *temp=q->next;
- node** r=&x->first;
- while (*r!=NULL && (*r)->no < q->no)
- r=&((*r)->next);
- q->next=(*r);
- *r=q;
- q=temp;
- }
- }
- bool comp(node *x,node *y)
- {
- if(x==NULL && y==NULL)
- return true;
- if(x==NULL || y==NULL)
- return false;
- if(x->no!=y->no)
- return false;
- node *p=x->first;
- node *q;
- while(p!=NULL)
- {
- q=y->first;
- while(q)
- {
- if(q->visited==false && comp(p,q))
- {
- q->visited=true;break;
- }
- else if(q->visited==false)
- {
- setvisit(q,false);
- }
- q=q->next;
- }
- if(q==NULL)
- return false;
- p=p->next;
- }
- return true;
- }
- inline void del(node *x)
- {
- if(x==NULL) return;
- node *p=x->first;
- while(p)
- {
- node* q=p->next;
- del(p);
- p=q;
- }
- delete p;
- x->first=NULL;
- }
- int main()
- {
- int t;
- node t1,t2;
- int n=0;
- bool flag;
- scanf("%d",&t);getchar();
- while(t--)
- {
- scanf("%s %s",w1,w2);
- n=0;creat(t1.first,w1,n);
- n=0;creat(t2.first,w2,n);
- cal(&t1);cal(&t2);
- tsort(&t1);tsort(&t2);
- flag=comp(&t1,&t2);
- del(&t1);del(&t2);
- if(flag) printf("Truen");
- else printf("Falsen");
- }
- return 0;
- }
TC初次
初次听说TopCoder是在大一的时候去ZJU的ZJG校区听薛在岳大牛的ACM讲座,时隔那么久,我还是小菜菜一个,不过今晚和一起参加ACM的室友一起注册了TC的SRM409。
C++还没学好,250分的题目我花了20分钟左右才写好,交了一下,147.93分。
challenge phase阶段,我成功cha掉房间里最菜的250,不过又失败了两次,就收手了,呵呵
发现Topcoder很有意思
ELF Hash
ELF hash是对字符串进行hash操作时的常用函数
这个算法将一个字符串的数组中的每个元素依次按前四位与上一个元素的低四位相与,组成一个长整形,如果长整的高四位大于零,那么就将它折回再与长整的低四位相异或,要注意最后得到的整型不是唯一的,一开始因为不知道这个吃过亏。
- int ELFhash(char *key)
- {
- unsigned long h = 0;
- unsigned long g;
- while( *key )
- {
- h =( h<< 4) + *key++;
- g = h & 0xf0000000L;
- if( g ) h ^= g >> 24;
- h &= ~g;
- }
- return h;
- }
Valentine's Day
Posted by twcai in Some parts on February 14, 2008
本来对这个Valentine’s Day都没啥感觉了,可今晚随便聊了几句她却突然跟我说:“情人节快乐。”
于是苦笑了一下,发现自己无法再对刚刚到来的这个情人节释怀了。
原来我还没有洒脱到有个本本,有群哥儿们就能笑对人生的地步;原来她在我心里还是占据着最重要的位置……可惜我要追的路还太远太远,都根本不能确定能否跟上她的脚步。
今天这个节日,她会怎么过?









