POJ 1416

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <cstdlib>

using namespace std;
int target;
string shredding;
class Node{
public:
    int mtimes1;
    string s1;
};
map<int, Node > M;

void dfs(int s1, const string &s2, string res){
    // 剪枝
    if (s1 > target)
        return;
    if (s2.length()==0){
        // 之前没有
        if (M.find(s1)==M.end()){
            Node node;
            node.mtimes1 = 1, node.s1 = res;
            M[s1] = node;
        } else{
            M[s1].mtimes1++;
        }
        return;
    }
    for (int i = 1; i <= s2.length(); ++i) {
        string s = s2.substr(0, i);
        dfs(s1+atoi(s.c_str()), s2.substr(i), res+" "+s);
    }
}

int main(){
//    freopen("../in.txt", "r", stdin);
    while (cin>>target>>shredding&&target){
        //如果不用切取
        if (target == atoi(shredding.c_str())){
            cout<<target<<" "<<target<<endl;
            continue;
        }
        M.clear();
        // other cases;
        for (int i = 1; i <= shredding.length() ; ++i) {
            string s = shredding.substr(0, i);
            // 前面的哪个数字固定,后面的继续分
            dfs(atoi(s.c_str()), shredding.substr(i), s);
        }
        if (M.size() == 0)
            cout<<"error"<<endl;
        else if (M.rbegin()->second.mtimes1>=2){
            cout<<"rejected"<<endl;
        } else{
            cout<<M.rbegin()->first<<" ";
            cout<<M.rbegin()->second.s1<<endl;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值