POJ 1635 Subway tree systems (BSOJ 1092)

题目大意:求两个有根数是否同构

方法:

1.from nodgd %%%%% 来自某次noip模拟赛的题解


2.最小表示法 没看懂……

3.利用hash判断

国家集训队论文《Hash在信息学竞赛中的一类应用》中有讲解。

直接上代码

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int mod=160883,mul=9110;
int T,a[10005],b[10005];
string s1,s2;
int dfs(int l,int r,int a[])
{
	if(l+1==r)return 1;
	int t,i,j,tot=0,tmp[3005],ret=1908;
	for(i=l;i<=r;i=j+1)
	{
		t=0;
		for(j=i;j<=r;j++)
		{
			t+=a[j];
			if(t==0)break;
		}
		tmp[++tot]=dfs(i+1,j-1,a);
	}
	sort(tmp+1,tmp+tot+1);
	for(int i=1;i<=tot;i++)ret=((ret*mul)^tmp[i])%mod;
	return ret;
}
int main(){
	cin>>T;
	while(T--)
	{
		cin>>s1>>s2;
		int l1=s1.length(),l2=s2.length();
		s1=' '+s1,s2=' '+s2;
		for(int i=1;i<=l1;i++)
		  if(s1[i]=='0')a[i]=1;
		  else a[i]=-1;
		for(int i=1;i<=l1;i++)
		  if(s2[i]=='0')b[i]=1;
		  else b[i]=-1;
		if(dfs(1,l1,a)==dfs(1,l2,b))cout<<"same\n";
		else cout<<"different\n";
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值