HDU 4403 A very hard Aoshu problem By Assassin 模拟

A very hard Aoshu problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1486 Accepted Submission(s): 1017

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

题解:这个怎么说呢,关键点在字符串不超过15,很小是可以暴力的。那么怎么暴力???我是这样做的。
1.首先我们是需要将字符串拆分,保证两边没有空串,中间就放等号了
2.那么对于一个串s,每个空挡位置只有两种可能,放+或者不放,我们用状压模拟!怎么模拟,比如说我有一个串1234,那么我们规定,如果当前位置状态是1,那么在此位置是加号,sum加上之前的数,将数跟新至当前位置的数值!(注意要从字符串的1位置开始,因为不会出现+1234这种数!这个很关键,如果串有n位,状态只有2^(n-1)种情况)比如说,现在状态是101,那么sum=1+23+4。**用两个map记录前后出现和的次数!
3.然后将前后串每种和位置都出现的相乘加到结果里!**注意这里用到了map的迭代器!

这是我自己意淫数来的想法,没想到1A,还不错,大家可以参考一下

#include <bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
using namespace std;
map<long long,long long>mp[2];
string s;
int two[16]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767};  //先把2**n-1情况数打出来 

void handle(string key,int pos){
    int i,j,k,len=key.size();
    for(k=0;k<=two[len-1];k++){    //!注意为什么是len-1,因为开头是不可以用加号的! 
        long long sum=0,tmp=0,value[16]={0};   //将k状态做成2进制存到数组中 
        for(i=k,j=len-1;j>=0;j--){
            value[j]=i&1;
            i>>=1;
        }
        for(i=0;i<len;i++){
            if(value[i]==1){      //如果是1结算更新 
                sum+=tmp;
                tmp=key[i]-'0';
            }
            else{                 //如果是0更新 
                tmp=tmp*10+key[i]-'0';
            }
        }
        sum+=tmp;                //别忘了最后可能遗漏最后一项 
        mp[pos][sum]++;
    }
}

int main(){
    int i,j,len;
    long long ans;
    while(cin>>s){
        if(s=="END")break;
        len=s.size();
        for(ans=0,i=1;i<len;i++){
            string s1,s2;
            s1.assign(s,0,i);
            s2.assign(s,i,len-i);
            mp[0].clear(); mp[1].clear();
            handle(s1,0);  handle(s2,1);
            map<long long,long long> :: iterator it;    //用迭代器找出都出现的情况,相乘后加入结果 
            for(it=mp[0].begin();it!=mp[0].end();it++){
                if(mp[1][it->first]!=0){
                    ans+=(mp[0][it->first]*mp[1][it->first]);
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值