POJ1635:Subway tree systems
链接:http://poj.org/problem?id=1635
填坑树同构
题目给出的是除根外的括号序列表示。
其实只要跟你说hash大家都能写得出来……
hash函数取个效果别太差的就行了吧
#include<vector> #include<cstdio> #include<cstring> #include<algorithm> #define MN 3111 using namespace std; int read_p,read_ca; inline int read(){ read_p=0;read_ca=getchar(); while(read_ca<'0'||read_ca>'9') read_ca=getchar(); while(read_ca>='0'&&read_ca<='9') read_p=read_p*10+read_ca-48,read_ca=getchar(); return read_p; } const int HA=1e4+7; int T,n,m,ha[MN],a,b; char s[MN],c[MN]; int dfs(char s[],int &p){ int mmh=ha[0]; vector <int> hash; for (p++;s[p]!='1';) hash.push_back(dfs(s,p)); p++; sort(hash.begin(),hash.end()); for (int i=0;i<hash.size();i++) mmh=(mmh^hash[i])*ha[i+1]%HA; return mmh; } int main(){ T=read(); for (int i=0;i<MN;i++) ha[i]=rand()%HA; while (T--){ scanf("%s%s",s+1,c+1);a=b=0; s[0]=c[0]='0';s[strlen(s+1)+1]='1';c[strlen(c+1)+1]='1'; puts(dfs(s,a)==dfs(c,b)?"same":"different"); } }