标签:暴力 hdu dfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403
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
Sample Output
Source
//思路:首先暴力出这个字符串中任意两个位置的表示的数的大小。
代码如下:
#include <cstdio>
#include <cstring>
char s[47];
int len,ans;
int Num[47][47];
void dfs_r(int lsum,int pos,int rsum)
{
if(pos==len)
{
if(lsum==rsum)
ans++;
return;
}
for(int i = pos; i < len; i++)
dfs_r(lsum,i+1,rsum+Num[pos][i]);
}
void dfs_l(int k,int mid,int lsum)
{
if(k==mid)
{
dfs_r(lsum,mid,0);
}
for(int i = k; i < mid; i++)
{
dfs_l(i+1,mid,lsum+Num[k][i]);
}
}
int main()
{
while(~scanf("%s",s))
{
if(strcmp(s,"END")==0)
break;
memset(Num,0,sizeof(Num));
len = strlen(s);
ans = 0;
for(int i = 0; i < len; i++)
{
for(int j = i; j < len; j++)
{
for(int k = i; k <= j; k++)
Num[i][j]=Num[i][j]*10+(s[k]-'0');
// 获得 位置为 i,j 的数的值
//printf("Num[%d][%d]: %d\n",i,j,Num[i][j]);
}
}
for(int i = 1; i < len; i++)//枚举等号的位置
{
dfs_l(0,i,0);
}
printf("%d\n",ans);
}
return 0;
}
HDU 4403 A very hard Aoshu problem(DFS+暴力)
标签:暴力 hdu dfs
原文地址:http://blog.csdn.net/u012860063/article/details/39104371