zoukankan      html  css  js  c++  java
  • 7-3 Path to Infinity(还没ac)

    留坑

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=14;
    const int mod=99991;
    typedef long long ll;
    string s,t;
    int tol1=0,tol2=0,tol3=0;
    int a[maxn];
    string tmp,path;
    unordered_set<string> st;
    typedef long long ll;
    int N,M;
    int tle=0;
    void dfs (int num,int sum) {
        //num表示当前已经用的数字数,sum表示当前表达式的值 
        if (num==N) {
            if (sum==M) {
                if (st.size()==0) path=tmp;
                st.insert(tmp);
            }
            return;
        }
        if (tol3) {
            for (int i=0;i<10;i++) {
                if (!a[i]) continue;
                tol3--;
                a[i]--;
                tmp.push_back('*');tmp.push_back(i+'0');
                dfs(num+1,sum*i);
                tol3++;
                tmp.pop_back();tmp.pop_back();
                a[i]++;
            }
        }
        if (tol1) {
            for (int i=0;i<10;i++) {
                if (!a[i]) continue;
                tol1--;
                a[i]--;
                tmp.push_back('+');tmp.push_back(i+'0');
                dfs(num+1,sum+i);
                tol1++;
                tmp.pop_back();tmp.pop_back();
                a[i]++;
            }
        }
        if (tol2) {
            for (int i=0;i<10;i++) {
                if (!a[i]) continue;
                tol2--;
                a[i]--;
                tmp.push_back('-');tmp.push_back(i+'0');
                dfs(num+1,sum-i);
                tol2++;
                tmp.pop_back();tmp.pop_back();
                a[i]++;
            }
        }
    }
    int main () {
        scanf("%d %d",&N,&M);
        cin>>s>>t;
        for (int i=0;i<t.length();i++) {
            if (t[i]=='+') tol1++;
            if (t[i]=='-') tol2++;
            if (t[i]=='*') tol3++;
        }
        for (int i=0;i<s.length();i++) {
            a[s[i]-'0']++;
        }
        for (int i=0;i<10;i++) {
            if (a[i]) {
                a[i]--;
                tmp.push_back(i+'0');
                dfs(1,i);
                a[i]++;
                tmp.pop_back();
            }
        }
        printf ("%d
    ",st.size());
        if (!st.size()) printf ("Impossible");
        else cout<<path;
        return 0;
    }
  • 相关阅读:
    微信开发 (一) 消息回复
    java文本获取
    Axis2开发webservice详解
    springmvc 统一处理异常
    easyui 动态添加input标签
    excel 导入
    eclipse启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space
    配置阿里云SLB全站HTTPS集群
    Nginx之HTTPS
    Nginx实现rewrite重写
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/12343891.html
Copyright © 2011-2022 走看看