HDU 4403 A very hard Aoshu problem(DFS)

本文介绍了一道简单的搜索题的解题思路,通过递归处理数值组合的可能性,并采用排序和二分查找来优化求解过程,确保算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403

比较简单的搜索,注意点细节就可以了!

每走下一步分两种情况,当前位置的值是独自成下一个数的开头还是和上面的一个结合,递归分两种情况就好了

其次就是枚举等号的位置,每次然后求出右边的所有情况的结果,排序下然后左边的结果在右边的集合里面找,

找到几个就是几个,加上就可以了,注意用long long


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define maxn 20
char str[maxn];
int rec[maxn];
int pos,global,ways,end;
long long ans[10000];
int find_ans(int len,long long other,long long now)
{
    if(len==global)
    {
        ans[pos++]=now+other;
        return 0;
    }
    find_ans(len+1,other,now*10+rec[len]);
    find_ans(len+1,other+now,rec[len]);
    return 0;
}
int solve(int len,long long other,long long now)
{

    if(len==end)
    {
        int k=now+other;
        ways+=upper_bound(ans,ans+pos,k)-lower_bound(ans,ans+pos,k);
        return 0;
    }
    solve(len+1,other,now*10+rec[len]);
    solve(len+1,other+now,rec[len]);
    return 0;
}
int main()
{
    int i,j,k,len;
    while(scanf("%s",str)!=EOF)
    {
        if(strcmp(str,"END")==0)
        return 0;
        len=strlen(str);
        global=len;
        ways=0;
        for(i=0;i<len;i++)
        rec[i]=str[i]-'0';
        for(i=1;i<=len-1;i++)
        {
            end=i;
            pos=0;
            find_ans(end+1,0,rec[end]);
            sort(ans,ans+pos);
            solve(1,0,rec[0]);
        }
        printf("%d\n",ways);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值