Algoogle

Algorithm for Programming Contest

AOJ 1155 How can I satisfy thee? Let me count the ways…

Category: AOJ Tag: parsing

How can I satisfy thee? Let me count the ways…

問題概要


解法


P,Q,Rの組み合わせは27組なので全部試す.
文字を見つけたら値に変えて, 閉じカッコを見つけたら遡って括弧内の計算をする.
‘-‘を見つけたらその計算をする

コード


(1155.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
#include <bits/stdc++.h>
#define rep(i, b) for(int i = 0; i < b; i++)
using namespace std;

int calc(int x, char c, int y){
    if( c == '-'){
        if(y == 0) return 2;
        else if(y == 1) return 1;
        else return 0;
    }
    if(c == '+'){
        if(x == 0) return y;
        else if(x == 1){
            if(y == 0) return x;
            else return y;
        }
        else if(x == 2) return x;
    }
    if(c == '*'){
        if(x == 0 || y == 0) return 0;
        else if(x == 1 || y == 1) return 1;
        else return 2;
    }
}

int main(){
    string str;
    while(cin >> str, str != "."){
        rep(i,str.size()){
            if(str[i] == '-' && str[i+1] == '-'){
                i = str.erase(str.begin() + i, str.begin() + i + 2) - str.begin();
            }
        }
        int cnt = 0;
        rep(p,3) rep(q,3) rep(r,3){
            string tmp = str;
            bool flag = true;
            while(1){
                int x = -1, y = -1;
                char c;
                int i = 0;
                while(tmp[i++] != ')'){
                    if(i >= tmp.size()){
                        flag = false;
                        break;
                    }
                }
                if(!flag)break;
                i--;
                tmp.erase(tmp.begin() + i, tmp.begin() + i + 1);
                int j = i;
                char command;
                while(tmp[j] != '('){
                    if(tmp[j] >= '0' && tmp[j] <= '2'){
                        if(y == -1) y = tmp[j] - '0';
                        else x = tmp[j] - '0';
                    }
                    else if(tmp[j] == 'P'){
                        if(y == -1) y = p;
                        else x = p;
                        tmp[j] = p + '0';
                    }
                    else if(tmp[j] == 'Q'){
                        if(y == -1) y = q;
                        else x = q;
                        tmp[j] = q + '0';
                    }
                    else if(tmp[j] == 'R'){
                        if(y == -1) y = r;
                        else x = r;
                        tmp[j] = r + '0';
                    }
                    else if(tmp[j] == '-') {
                        char t = calc(0,'-',tmp[j+1]-'0') + '0';
                        tmp.replace(tmp.begin() + j, tmp.begin() + j + 2, 1, t);
                        if(x != -1) x = t - '0';
                        else y = t - '0';
                    }
                    else if(tmp[j] == '+' || tmp[j] == '*') command = tmp[j];
                    j--;
                }
                tmp.erase(tmp.begin() + j,tmp.begin() + j + 1);
                char t = calc(x,command,y) + '0';
                tmp.replace(tmp.begin() + j, tmp.begin() + j + 3, 1, t);
            }
            int y = -1;
            if( tmp[0] < '0' || tmp[0] > '2'){
                if(tmp[tmp.size()-1] == 'P') tmp[tmp.size()-1] = p + '0';
                else if(tmp[tmp.size()-1] == 'Q') tmp[tmp.size()-1] = q + '0';
                else if(tmp[tmp.size()-1] == 'R') tmp[tmp.size()-1] = r + '0';

                if(tmp[0] == '-'){
                    char t = calc(0,'-',tmp[1]-'0') + '0';
                    tmp.replace(tmp.begin(), tmp.begin() + 2, 1, t);
                }
            }
            if(tmp == "2") cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

Comments