Hướng dẫn giải của EXPAR


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của ladpro98

#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, a, b) for(int i = (a); i <=(b); i++)
#define FORD(i, a, b) for(int i = (a); i > (b); i--)
#define REPD(i, a, b) for(int i = (a); i >=(b); i--)
#define SZ(a) (int((a).size()))
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair
#define LL long long
#define LD long double
#define II pair<int, int>
#define X first
#define Y second
#define VI vector<int>

using namespace std;
string s, e, px, py;
char x, y;

bool isDigit(char x) {return '0' <= x && x <= '9';}
bool is0(char x) {return isDigit(x) && (x - '0' & 1) == 0;}
int parseInt(string s)
    {int a = 0; FOR(i, 0, SZ(s)) a = 10 * a + s[i] - '0'; return a;}

int main() {
    ios :: sync_with_stdio(0); cin.tie(0);
    cin >> s; cin >> px; cin >> py;
    if (px == "Odd") x = '1'; else x = '0';
    if (py == "Odd") y = '1'; else y = '0';
    int n = SZ(s);
    FOR(i, 0, n) {
        if (s[i] == 'x') s[i] = x;
        if (s[i] == 'y') s[i] = y;
    }
    e = "+";
    for(int i = 0; i < SZ(s); ) {
        if (isDigit(s[i])) {
            int j = i;
            while (j < SZ(s) - 1 && isDigit(s[j + 1])) j++;
            e += char('0' + (parseInt(s.substr(i, j - i + 1)) & 1));
            i = j + 1;
        }
        else {
            if (s[i] == '-') s[i] = '+';
            e += s[i];
            i++;
        }
    }
    e += '+';
    //cout << e << endl;
    int ans = 0;
    for(int i = 0; i < SZ(e) - 1; )
    if (e[i] == '+') {
        int j = i + 1;
        while (e[j] != '+') j++;
        bool isOdd = 1;
        FOR(k, i, j) if (is0(e[k])) {
            isOdd = 0; break;
        }
        ans += isOdd;
        i = j;
    }
    else i++;
    if (ans & 1) cout << "Odd"; else cout << "Even";
    return 0;
}

Code mẫu của RR

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <complex>
#include <iostream>
#include <algorithm>

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>
using namespace std;

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define SZ(S) ((int) ((S).size()))

#define DEBUG(x) { cout << #x << " = " << x << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }

int main() {
    ios :: sync_with_stdio(false); cin.tie(NULL);
    cout << (fixed) << setprecision(6);
    string s, a, b;
    while (cin >> s >> a >> b) {
        int x = (a[0] == 'O') ? 1 : 0;
        int y = (b[0] == 'O') ? 1 : 0;

        REP(i,s.length()) if (s[i] == '+' || s[i] == '-') s[i] = ' ';
        REP(i,s.length()) if (s[i] == 'x') s[i] = '0' + x;
        REP(i,s.length()) if (s[i] == 'y') s[i] = '0' + y;

        string t;
        stringstream ss(s);
        int res = 0;
        while (ss >> t) {
            REP(i,t.length()) if (t[i] == '*') t[i] = ' ';
            stringstream ss2(t);

            int prod = 1;
            string cur;
            while (ss2 >> cur) {
                prod = (prod * (cur[cur.size()-1] - '0')) % 2;
            }
            res = (res + prod) % 2;
        }
        if (res % 2) cout << "Odd"; else cout << "Even";
        cout << endl;
    }
    return 0;
}

Code mẫu của skyvn97

#include<bits/stdc++.h>
#define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1)
using namespace std;
const string odd="Odd";
const string even="Even";
queue<char> inp;
bool loadExpLv1(void) {
    assert(!inp.empty());
    char c=inp.front();inp.pop();
    assert(c=='0'||c=='1');
    return (c=='1');
}
bool loadExpLv2(void) {
    bool res=loadExpLv1();
    while (!inp.empty() && inp.front()=='&') {
        inp.pop();
        res&=loadExpLv1();
    }
    return (res);
}
bool loadExpLv3(void) {
    bool res=loadExpLv2();
    while (!inp.empty() && inp.front()=='^') {
        inp.pop();
        res^=loadExpLv2();
    }
    return (res);
}
int main(void) {
    string s,x,y;
    cin>>s>>x>>y;
    REP(i,s.size()) {
        if ('0'<=s[i] && s[i]<='9') {
            if (i+1<s.size() && '0'<=s[i+1] && s[i+1]<='9') continue;
            inp.push((s[i]-'0')%2+'0');
        }
        if (s[i]=='x') inp.push((x==odd)+'0');
        if (s[i]=='y') inp.push((y==odd)+'0');
        if (s[i]=='+' || s[i]=='-') inp.push('^');
        if (s[i]=='*') inp.push('&');
    }
    cout<<(loadExpLv3()?odd:even);
    return 0;
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.