首页 > 其他 > 详细

HDOJ 题目A very hard Aoshu problem(暴力,DFS)

时间:2015-03-27 17:20:54      阅读:207      评论:0      收藏:0      [点我收藏+]

A very hard Aoshu problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 943    Accepted Submission(s): 634


Problem Description
Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a problem to test his students:

Given a serial of digits, you must put a ‘=‘ and none or some ‘+‘ between these digits and make an equation. Please find out how many equations you can get. For example, if the digits serial is "1212", you can get 2 equations, they are "12=12" and "1+2=1+2". Please note that the digits only include 1 to 9, and every ‘+‘ must have a digit on its left side and right side. For example, "+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and "11+1=12" are different equations.
 

Input
There are several test cases. Each test case is a digit serial in a line. The length of a serial is at least 2 and no more than 15. The input ends with a line of "END".
 

Output
For each test case , output a integer in a line, indicating the number of equations you can get.
 

Sample Input
1212 12345666 1235 END
 

Sample Output
2 2 0
 

Source
 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:  5193 5192 5191 5190 5189 
 
ac代码
#include<stdio.h>
#include<string.h>
char str[20];
int num[20][20];
int ans=0,len;
void dfs_r(int lsum,int pos,int rsum)
{
	if(lsum==rsum&&pos==len)
	{
		ans++;
		return;
	}
	for(int i=pos;i<len;i++)
	{
		dfs_r(lsum,i+1,rsum+num[pos][i]);
	}
}
void dfs_l(int pos,int mid,int lsum)
{
	if(pos==mid)
	{
		dfs_r(lsum,mid,0);
	}
	for(int i=pos;i<mid;i++)
	{
		dfs_l(i+1,mid,lsum+num[pos][i]);
	}
}
int main()
{
	while(scanf("%s",str)!=EOF)
	{
		int i,j,k;
		if(strcmp(str,"END")==0)
			break;
		len=strlen(str);
		memset(num,0,sizeof(num));
		for(i=0;i<len;i++)
		{
			for(j=i;j<len;j++)
			{
				for(k=i;k<=j;k++)
				{
					num[i][j]=num[i][j]*10+(str[k]-'0');
				}
			//	printf("%d %d %d\n",i,j,num[i][j]);
			}
		}
		ans=0;
		for(i=1;i<len;i++)
		{
			dfs_l(0,i,0);
		}
		printf("%d\n",ans);
	}
}


HDOJ 题目A very hard Aoshu problem(暴力,DFS)

原文:http://blog.csdn.net/yu_ch_sh/article/details/44679373

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!