noip2012 阅读程序4

#include<cstdio>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<ctime>
#include<iostream>
using namespace std;
const int maxn=1000;
int lefts[20],rights[20],father[20];
string s1,s2,s3;
int n,ans;

void calc(int x,int dep)
{
	ans+=dep*(s1[x]-'A'+1);
	if(lefts[x]>=0) calc(lefts[x],dep+1);
	if(rights[x]>=0) calc(rights[x],dep+1);
}

void check(int x)
{
	if(lefts[x]>=0) check(lefts[x]);
	s3=s3+s1[x];
	if(rights[x]>=0) check(rights[x]);
}

void dfs(int x,int th)
{
	if(th==n)
	{
		s3="";
		check(0);
		if(s3==s2)
		{
			ans=0;
			calc(0,1);
			cout<<ans<<endl;
		}
		return;
	}
	if(lefts[x]==-1&& rights[x]==-1)
	{
		lefts[x]=th;
		father[th]=x;
		dfs(th,th+1);
		father[th]=-1;
		lefts[x]=-1;
	}
	if(rights[x]==-1)
	{
		rights[x]=th;
		father[th]=x;
		dfs(th,th+1);
		father[th]=-1;
		rights[x]=-1;
	}
	if(father[x]>=0)
	{
		dfs(father[x],th);
	}
}

int main()
{
#ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
#endif
	int i,j;
	cin>>s1;
	cin>>s2;
	n=s1.size();
	memset(lefts,-1,sizeof(lefts));
	memset(rights,-1,sizeof(rights));
	memset(father,-1,sizeof(father));
	dfs(0,1);
	//printf("%.2lf\n",(double)clock()/CLOCKS_PER_SEC);
  return 0;
}

/*
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值