• uva817 According to Bartjens


    uva817 According to Bartjens

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=100945#problem/C

    #include<bits/stdc++.h>
    #define REP(i,a,b) for(int i=a;i<=b;i++)
    #define MS0(a) memset(a,0,sizeof(a))
    #define PII pair<int,int>
    
    using namespace std;
    
    typedef long long ll;
    const int maxn=1000100;
    const int INF=(1<<29);
    
    char s[maxn];int ls;
    int f[maxn];
    int n;
    set<string> ans;
    char Fs[6]={'=','+','-','*'};
    
    bool judge()
    {
        if(s[0]=='0'&&f[1]==0) return 0;
        REP(i,1,n-2){
            if(f[i]&&s[i]=='0'){
                if(f[i+1]==0) return 0;
            }
        }
        int cnt=0;
        REP(i,1,n-1) cnt+=f[i];
        return cnt>0;
    }
    
    void update()
    {
        if(!judge()) return;/// pre_0
        stack<int> st,sf;
        int tmp=0;
        vector<int> v;
        REP(i,0,n-2){
            tmp=tmp*10+s[i]-'0';
            if(f[i+1]){
                v.push_back(tmp);
                tmp=0;
                v.push_back(-f[i+1]);
            }
        }
        tmp=tmp*10+s[n-1]-'0';
        v.push_back(tmp);
        for(int i=0;i<v.size();i++){
            tmp=v[i];
            if(tmp>=0){
                while(!sf.empty()&&sf.top()==-3){
                    tmp*=st.top();st.pop();sf.pop();
                }
                st.push(tmp);
            }
            else sf.push(v[i]);
        }
        stack<int> fst,fsf;
        while(!st.empty()) fst.push(st.top()),st.pop();
        while(!sf.empty()) fsf.push(sf.top()),sf.pop();
        tmp=fst.top();fst.pop();
        while(!fsf.empty()){
            if(fsf.top()==-1) tmp+=fst.top();
            else tmp-=fst.top();
            fsf.pop();fst.pop();
        }
        if(tmp==2000){
            string res="";
            res+=s[0];
            REP(i,1,n-1){
                if(f[i]) res+=Fs[f[i]];
                res+=s[i];
            }
            //if(res=="2100-100")
            //cout<<res<<"="<<tmp<<" "<<jud()<<endl;
            ans.insert(res);
        }
    }
    
    void dfs(int cur)
    {
        //cout<<cur<<" "<<n<<endl;
        if(cur==n){
            update();
            return;
        }
        //cout<<cur<<" "<<n<<endl;
        REP(i,0,3){
            f[cur]=i;
            dfs(cur+1);
        }
    }
    
    int main()
    {
        freopen("in.txt","r",stdin);
        //cout<<(int)'+'<<" "<<(int)'-'<<" "<<(int)'*'<<endl;
        int casen=1;
        while(~scanf("%s",s)&&s[0]!='='){
            n=strlen(s);
            s[n-1]='';n--;
            //cout<<s<<" "<<n<<endl;
            MS0(f);
            ans.clear();
            dfs(1);
            //sort(ans.begin(),ans.end());
            printf("Problem %d
    ",casen++);
            //for(int i=0;i<ans.size();i++) cout<<"  "<<ans[i]<<'='<<'
    ';
            for(set<string>::iterator it=ans.begin();it!=ans.end();++it) cout<<"  "<<(*it)<<'='<<'
    ';
            if((int)ans.size()==0) cout<<"  IMPOSSIBLE"<<'
    ';
        }
        return 0;
    }
    /**
    题意:
        给一个串n位的数字(n<=9),在这些数字的间隙选择性地插入一些'+','-'或'*',
        使之构成的表达式的值等于2000,如果有多组解,按字典序输出。
    分析:
        总共也就8个间隙,每个间隙有'+','-','*'和不插运算符4种情况,直接dfs枚举然后判断即可。
        对之后的判断求值需要用栈来实现。
    类型:
        枚举。
    注意事项:
        把栈的求值想清楚在写。
    坑点:
        1,给uva的格式跪了。。。之前几道题回车后的两个空格都是直接忽略,这次又不忽略了。。。
        2,字典序啊字典序。。。。没说清楚是表达式的字典序还是插入运算符的字典序。。。
        3,2000=这种情况没插入运算符算不合法。。。。在这个点浪费了40多分钟,为何如此坑我。。。
    总结:
        坑点一般出现在极端边缘小数据中。
    */
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    Singleton 单例模式 泛型 窗体控制
    SpringMVC异常处理注解@ExceptionHandler
    如何实现beanutils.copyProperties不复制某些字段
    Spring 注解学习笔记
    使用客户端SVN在Update时遇到Previous operation has not finished; run 'cleanup' if it was interrupted,需要cheanup文件夹,解决方式
    jQuery的选择器中的通配符
    mysql str_to_date字符串转换为日期
    Mybatis中mapper.xml文件判断语句中的单双引号问题
    Jquery中each的三种遍历方法
    Javascript 中动态添加 对象属性
  • 原文地址:https://www.cnblogs.com/--560/p/5005977.html
Copyright © 2020-2023  润新知