Algoogle

Algorithm for Programming Contest

AOJ 2255 6/2(1+2)

Category: AOJ Tag: parsing

6/2(1+2)

問題概要


解法


各演算子を計算する順番について深さ優先探索して,
最終的に数字だけになったらsetに突っ込む.

カッコの扱いが面倒くさかった印象.
演算子の隣にカッコがあったらその段階ではその演算子は計算しない.
カッコが消せるときはガンガン消していく.
(< num >)
みたいな場合. 多重カッコも

コード


(2255.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <algorithm>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#define rep(i,a) for(int i = 0;i < (a); i++)
#define all(u) (u).begin(),(u).end()
using namespace std;

//-1はm1とする
int ston(string num){
    int ret = 0;
    bool minus = false;
    if(num[0] == 'm') minus = true;
    rep(i,num.size()){
        if(num[i]=='m') continue;
        ret *= 10;
        ret += num[i] -'0';
    }
    if(minus) ret *= -1;
    return ret;
}

string ntos(int num){
    string ret = "";
    bool minus = false;
    if(num<0){
        minus = true;
        num *= -1;
    }
    if(num==0) return "0";

    while(num){
        ret += num%10 + '0';
        num /= 10;
    }
    if(minus) ret += 'm';
    reverse(all(ret));
    return ret;
}

bool isnum(char c){
    return ((c>='0' && c <='9') || c=='m');
}

int calc(int a, int b, char c){
    if(c=='+') return a+b;
    if(c=='*') return a*b;
    if(c=='/') return a/b;
    if(c=='-') return a-b;
}

void solve(string s){

    set<string> ans;
    stack<string> dfs;
    dfs.push(s);
    while(!dfs.empty()){
        string str = dfs.top();
        dfs.pop();
        rep(i,str.size()){
            if(str[i]=='('){
                int j = i+1;
                bool flag = true;
                int kakko = 1;
                bool che = false;
                while(kakko){
                    if(str[j] == '(') kakko++;
                    if(str[j] == ')'){
                        kakko--;
                        che = true;
                    }
                    if(che && str[j]!=')'){
                        flag = false;
                        break;
                    }
                    if(str[j]=='+' || str[j]=='-' || str[j]=='*' || str[j]=='/'){
                        flag = false;
                        break;
                    }
                    j++;
                }
                if(flag){
                    int k = i;
                    str.erase(str.begin()+k,str.begin()+k+1);
                    int kakko = 1;
                    while(kakko){
                        if(str[k] == '(') kakko++;
                        if(str[k] == ')') kakko--;
                        if(str[k]=='(' || str[k]==')') str.erase(str.begin()+k,str.begin()+k+1);
                        else k++;
                    }
                }
            }
        }
        bool ok = true;

        rep(j,str.size()){
            if(str[j]=='+' || str[j]=='-' || str[j]=='*' || str[j]=='/'){
                ok = false;
                break;
            }
        }

        if(ok){
            ans.insert(str);
            goto roop;
        }

        rep(i,str.size()){
            if((str[i]=='+' || str[i]=='-' || str[i]=='*' || str[i]=='/') &&
               isnum(str[i-1]) && isnum(str[i+1])){
                string args[2] = {};
                int nums[2];
                int cur = i-1, left, right;
                while(cur>=0 && isnum(str[cur])) args[0] += str[cur--];
                reverse(all(args[0]));
                left = ++cur;
                cur = i+1;

                while(cur<str.size() && isnum(str[cur])) args[1] += str[cur++];
                right = --cur;

                nums[0] = ston(args[0]);
                nums[1] = ston(args[1]);

                if(nums[1]==0 && str[i]=='/') continue;

                if(left>0 && right<str.size()-1){
                    if(str[left-1]=='(' && str[right+1]==')'){
                        left--;
                        right++;
                    }
                }
                string tmp = str;
                string b = ntos(calc(nums[0],nums[1],str[i]));
                tmp.replace(tmp.begin()+left,tmp.begin()+right+1,b);
                dfs.push(tmp);
            }
        }
    roop:;
    }
    cout << ans.size() << endl;
}

int main(){
    string str;
    while(cin>>str,str!="#"){
        solve(str);
    }
}

Comments