2015
05-24

# A very hard Aoshu problem

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.

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".

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".

1212
12345666
1235
END

2
2
0

n很小，可以考虑爆搜。但是不好写，因为没想到做预处理，将能构成的所有数先保存，然后直接爆搜。当然，还是可以剪枝的。

#include <iostream>
#include <string>
using namespace std;
const int maxn = 15;
string str;
int num[maxn][maxn];
int ans;
int len;

void get_num(int n)
{
for(int i = 0; i < n; i++)
{
int temp = 0;
for(int j = i; j < n; j++)
{
temp += str[j] - '0';
num[i][j] = temp;
temp *= 10;
}
}
}
void dfs_right(int s,int A,int sum,int len)
{
if(s == len )
{
if(A == sum)
{
ans++;
return ;
}
}
if(A < sum) return ;
for(int i = s; i < len; i++)
{
dfs_right(i+1,A,sum+num[s][i],len);
}
}
void dfs_left(int s,int sum,int mid)
{
if(s == mid)
{
dfs_right(mid,sum,0,len);
}
for(int i = s; i < mid; i++)
{
dfs_left(i+1,sum+num[s][i],mid);
}
}
void print_num()
{
for(int i = 0; i < len; i++)
{
for(int j = 0; j < len; j++) cout<<num[i][j]<<" ";
cout<<endl;
}
}
int main()
{
while(cin>>str)
{
if(str[0] == 'E') break;
len = str.size();
get_num(len);
//print_num();
ans = 0;
for(int i = 0; i < len-1; i++) dfs_left(0,0,i+1);
cout<<ans<<endl;
}
return 0;
}


1. 现在的人可能不知道，神经病有所好转以前是魔兽世界贴吧的吧主，那时候还没有微博，他就相当于网络红人的前二十了。不过是网红总会过气