Constraint Checker——hihoCoder176

描述

Given a set of constraints like 0<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.


这几天在广州出差,没时间做,今天出差回来赶快把最新的做完了。

怎么说呢?我的思路是先把限制条件存到string数组中,然后再将给出的字母对应的数字存到数组中,然后判断是否符合限制条件。

这一题主要是注意下在判断限制条件的时候还存在着数字,而不是用字母的形式给出,并且这个数字可能是多位数。我觉得我做的有点麻烦,但是先把代码po出来吧,不优化了。

string cons[22];
map<char, int> char2num;
int n, t, k=0;

void findSum(){
    for(int i = 0; i<n; i++){
        for(auto it = cons[i].begin(); it != cons[i].end(); it++){
            if(isupper(*it)){
                if(char2num.count(*it) == 0){
                    char2num[*it] = k++;
                }
            }
        }
        
    }
}

bool judge(int (&temp)[100]){
    for(int i = 0; i<n; i++){
        int t1 = 0, t2 = 0;
        string op;
        auto it = cons[i].begin();
        if(isupper(*it)){
            t1 = temp[char2num[*it]];
            it++;
        }
        else{
            while(isnumber(*it)){
                t1 = t1*10;
                t1 += *it - '0';
                it++;
            }
        }
        
        for(; it != cons[i].end(); it++){
            if(isupper(*it) || isnumber(*it)){
                if(isupper(*it))
                    t2 = temp[char2num[*it]];
                else{
                    while(isnumber(*it)){
                        t2 *= 10; t2 += *it - '0';
                        it++;
                    }
                    it--;
                }
                if(op == "<" && t1 < t2){
                    op.clear();
                    t1 = t2;
                    t2 = 0;
                }
                else if(op == "<=" && t1 <= t2){
                    op.clear();
                    t1 = t2;
                    t2 = 0;
                }
                else return false;
            }
            else{
                op += *it;
            }
        }
        
    }
    return true;
}


int main(){
    cin>>n;
    for(int i = 0; i<n; i++){
        cin>>cons[i];
        findSum();
    }
    cin>>t;
    for(int i = 0; i<t; i++){
        int charnum[100] = {0};
        char temp; int num;
        for(int p = 0; p<k; p++){
            cin>>temp>>num;
            charnum[char2num[temp]] = num;
        }
        if(judge(charnum)) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    
    return 0;
}

注意:hihoCoder的oj不能include<cctype>,所以不能用isupper( )、Isnumber( ),这个要自己写下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值