HDU 4403 A very hard Aoshu problem (DFS+状压)

来源:互联网 发布:windows搭建hadoop 编辑:程序博客网 时间:2024/06/16 16:42

A very hard Aoshu problem

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


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
1212123456661235END
 

Sample Output
220
 

Source
2012 ACM/ICPC Asia Regional Jinhua Online 
 



题意:

加一个等号,和任意加号,使等式成立。


POINT:

长度很小,直接暴力,可以状态压缩DFS,写起来会很清楚。



#include <stdio.h>#include <map>#include<iostream>#include <math.h>#include <algorithm>#include <string.h>#include <vector>#include <queue>#include <set>using namespace std;typedef long long  LL;const int maxn = 22;int a[maxn];int l;int ans=0;void haha(int t1,int t2,int deng){    int aa=0,bb=0;    int temp=a[1];    for(int i=2;i<=deng;i++){        if(t1&(1<<(i-2))){            aa+=temp;            temp=a[i];        }else{            temp=temp*10;            temp+=a[i];        }    }    aa+=temp;    temp=a[deng+1];    for(int i=deng+2;i<=l;i++){        if(t2&(1<<(i-deng-2))){            bb+=temp;            temp=a[i];        }else{            temp=temp*10;            temp+=a[i];        }    }    bb+=temp;    if(aa==bb) ans++;}int main(){    char s[22];    while(~scanf("%s",s)){        if(!strcmp(s, "END")){            break;        }else{            ans=0;            l=strlen(s);            for(int i=0;i<l;i++){                a[i+1]=s[i]-'0';            }            for(int i=1;i<l;i++){                int t1=1<<(i-1);                int t2=1<<(l-i-1);                for(int z=0;z<t1;z++){                    for(int x=0;x<t2;x++){                        haha(z,x,i);                    }                }            }            printf("%d\n",ans);        }    }    return 0;}



原创粉丝点击