A very hard Aoshu problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 664 Accepted Submission(s): 461
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.
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
2012 ACM/ICPC Asia Regional Jinhua Online
解题思路:去年金华网赛的一道题,很显然也是一道状压搜索,和今年的杭州A题有类似的地方,我自己状压写的不熟练,所以用来练练手
解题思路:去年金华网赛的一道题,很显然也是一道状压搜索,和今年的杭州A题有类似的地方,我自己状压写的不熟练,所以用来练练手
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int main()
{
int i,j,k,n,len,ans,pos[20];
char str[20];
bool flag[20];
while(~scanf("%s",str))
{
if(str[0]=='E')
break;
getchar();
ans=0;
len=strlen(str);
str[len]='\0';
n=(1<<(len-1));//对长度为len的数字串中间有len-1的空格,枚举添加或者不添加符号,共2^(len-1)种可能
for(i=1;i<n;i++)
{
memset(flag,false,sizeof(flag));
for(j=0;j<len-1;j++)
if(i&(1<<j))
flag[j]=true;//记录添加符号的位置
for(j=0;j<len-1;j++)
//枚举等号的位置
{
if(!flag[j])
continue;
memset(pos,-1,sizeof(pos));
int t=1;
for(k=0;k<len-1;k++)
{
if(flag[k]&&k!=j)
pos[t++]=k;//记下确定等号后加号的位置
}
if(t!=1)
pos[t]=100;
else
//对只有一个等号的情况进行特判
{
long long lef=0;
for(int p=0;p<=j;p++)
{
lef*=10;
lef+=str[p]-'0';
}
long long rig=0;
for(int p=j+1;p<len;p++)
{
rig*=10;
rig+=str[p]-'0';
}
if(lef==rig)
ans++;
break;
}
long long lef=0;
for(k=0;pos[k]<j;k++)
{
long long tmp=0;
for(int p=pos[k]+1;p<=min(pos[k+1],j);p++)
{
tmp*=10;
tmp+=str[p]-'0';
}
lef+=tmp;
}
long long rig=0;
//lef,rig记录等号左右的值之和分别是多少
for(int p=j+1;p<=min(pos[k],len-1);p++)
{
rig*=10;
rig+=str[p]-'0';
}
for(;pos[k]<len;k++)
{
long long tmp=0;
for(int p=pos[k]+1;p<=min(pos[k+1],len-1);p++)
{
tmp*=10;
tmp+=str[p]-'0';
}
rig+=tmp;
}
if(lef==rig)
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}