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]='\0';n--;
        //cout<<s<<" "<<n<<endl;
        MS0(f);
        ans.clear();
        dfs(1);
        //sort(ans.begin(),ans.end());
        printf("Problem %d\n",casen++);
        //for(int i=0;i<ans.size();i++) cout<<"  "<<ans[i]<<'='<<'\n';
        for(set<string>::iterator it=ans.begin();it!=ans.end();++it) cout<<"  "<<(*it)<<'='<<'\n';
        if((int)ans.size()==0) cout<<"  IMPOSSIBLE"<<'\n';
    }
    return 0;
}
/**
题意:
    给一个串n位的数字(n<=9),在这些数字的间隙选择性地插入一些'+','-'或'*',
    使之构成的表达式的值等于2000,如果有多组解,按字典序输出。
分析:
    总共也就8个间隙,每个间隙有'+','-','*'和不插运算符4种情况,直接dfs枚举然后判断即可。
    对之后的判断求值需要用栈来实现。
类型:
    枚举。
注意事项:
    把栈的求值想清楚在写。
坑点:
    1,给uva的格式跪了。。。之前几道题回车后的两个空格都是直接忽略,这次又不忽略了。。。
    2,字典序啊字典序。。。。没说清楚是表达式的字典序还是插入运算符的字典序。。。
    3,2000=这种情况没插入运算符算不合法。。。。在这个点浪费了40多分钟,为何如此坑我。。。
总结:
    坑点一般出现在极端边缘小数据中。
*/
View Code

 

posted @ 2015-11-29 23:44  __560  阅读(253)  评论(0编辑  收藏  举报