hihoCoder176

Constraint Checker
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Given a set of constraints like 0 < <script type="math/tex" id="MathJax-Element-269"><</script>N<=M<=100 and values for all the variables, write a checker program to determine if the constraints are satisfied.

More precisely, the format of constraints is:

token op token op … op token

where each token is either a constant integer or a variable represented by a capital letter and each op is either less-than ( < ) or less-than-or-equal-to ( <= ).

输入
The first line contains an integer N, the number of constraints. (1 ≤ N ≤ 20)

Each of the following N lines contains a constraint in the previous mentioned format.

Then follows an integer T, the number of assignments to check. (1 ≤ T ≤ 50)

Each assignment occupies K lines where K is the number of variables in the constraints.

Each line contains a capital letter and an integer, representing a variable and its value.

It is guaranteed that:

  1. Every token in the constraints is either an integer from 0 to 1000000 or an variable represented by a capital letter from ‘A’ to ‘Z’.

  2. There is no space in the constraints.

  3. In each assignment every variable appears exactly once and its value is from 0 to 1000000.

输出
For each assignment output Yes or No indicating if the constraints are satisfied.

样例输入
2
A < <script type="math/tex" id="MathJax-Element-270"><</script>B<=E
3<=E<5
2
A 1
B 2
E 3
A 3
B 5
E 10
样例输出
Yes
No

题目意思就是给一定数量的对变量的约束条件,然后给出现过的所有变量进行赋值操作,最后看这些赋值是否满足所给的约束条件。

思路:题目里说只有<或者<=这样的约束,那么可以简单的降每条约束拆分成两条,然后对于赋值的话用map来存,最后遍历整个拆分过后的约束,就直接在map里查找比较两个值的大小就行了。要注意的是讲出现过的变量放入set中,将数字的字符串转换成十进制。


#include <bits/stdc++.h>

using namespace std;

int f(string s)
{
    int len = s.length();
    int res = 0;
    for(int i=0;i<len-1;i++){
        res = (res+s[i]-'0')*10;

    }

    res += s[len-1]-'0';
    return res;
}


int main() {
    int n;
    cin >> n;
    vector<string> constrains;
    while (n--) {
        string temp;
        cin >> temp;
        constrains.push_back(temp);
    }

    vector<vector<string> > token(constrains.size(), vector<string>{});
    vector<vector<string> > ops(constrains.size(), vector<string>{});
    set<string> vars;
    for (int i = 0; i < constrains.size(); i++) {
        string temp = "";
        for (int j = 0; j < constrains[i].size(); j++) {
            if (constrains[i][j] != '<') temp += constrains[i][j];
            else if (j < constrains[i].size() - 1 && constrains[i][j+1] == '=') {
                token[i].push_back(temp);
                if (temp.size() == 1 && temp >= "A" && temp <= "Z") vars.insert(temp);
                ops[i].push_back("<=");
                j++;
                temp = "";
            }
            else {
                if (temp.size() == 1 && temp >= "A" && temp <= "Z") vars.insert(temp);
                token[i].push_back(temp);
                ops[i].push_back("<");
                temp = "";
            }
        }
        if (temp.size() == 1 && temp >= "A" && temp <= "Z") vars.insert(temp);
        token[i].push_back(temp);
    }
    int times;
    cin >> times;
    while (times--) {
        unordered_map<string, int> dict;
        for (int i = 0; i < vars.size(); i++) {
            string te1; cin >> te1;
            int te2; cin >> te2;
            dict[te1] = te2;
        }
        bool flag = 1;
        for (int i = 0; i < ops.size(); i++) {
            for (int j = 0; j < ops[i].size(); j++) {
                string te1 = token[i][j], te2 = token[i][j + 1];
                int num1, num2;
                if (te1.size() == 1 && te1 >= "A" && te1 <= "Z") num1 = dict[te1];
                else num1 = f(te1);
                if (te2.size() == 1 && te2 >= "A" && te2 <= "Z") num2 = dict[te2];
                else num2 = f(te2);
                if (ops[i][j] == "<" && num1 >= num2) flag = 0;
                if (ops[i][j] == "<=" && num1 > num2) flag = 0;
            }
        }
        if (flag) cout << "Yes" << endl;
        else cout << "No" << endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值