[HihoCoder]#1341 : Constraint Checker

华电北风吹
天津大学认知计算与应用重点实验室
2016-07-17

题目链接:
http://hihocoder.com/problemset/problem/1341

题目分析:
题目比较简单,一个一个匹配即可。

参考代码:

#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <sstream>
using namespace std;

int data[26];

int CountLetter(vector<string> & constrain)
{
    bool hasLetter[26];
    memset(hasLetter, false, sizeof(hasLetter));
    for (int i = 0; i < constrain.size(); i++)
    {
        for (int j = 0; j < constrain[i].size(); j++)
        {
            int id = constrain[i][j] - 'A';
            if ((id >= 0) && (id < 26))
            {
                hasLetter[id] = true;
            }
        }
    }
    int count = 0;
    for (int i = 0; i < 26; i++)
    {
        count += hasLetter[i] ? 1 : 0;
    }
    return count;
}

void split(string &s, string &delim, vector<string>& ret)
{
    size_t last = 0;
    size_t index = s.find_first_of(delim, last);
    while (index != string::npos)
    {
        ret.push_back(s.substr(last, index - last));
        last = index + 1;
        index = s.find_first_of(delim, last);
    }
    if (index - last>0)
    {
        ret.push_back(s.substr(last, index - last));
    }
}

int GetValue(string s)
{
    if (s[0] == '=')
    {
        s = s.substr(1);
    }
    if (s.size() > 1)
    {
        stringstream ss;
        ss << s;
        int m;
        ss >> m;
        return m;
    }
    else
    {
        int id = s[0] - 'A';
        if (id>=0)
        {
            return data[id];
        }
        else
        {
            return s[0] - '0';
        }
    }
}

bool CheckString(string &s)
{
    string delim = "<";
    vector<string> ret;
    split(s, delim, ret);
    for (int i = 0; i < ret.size() - 1; i++)
    {
        bool equal = false;
        if (ret[i + 1][0] == '=')
            equal = true;
        int v1 = GetValue(ret[i]);
        int v2 = GetValue(ret[i + 1]);
        if (v1 > v2)
        {
            return false;
        }
        if ((v1 == v2) && (equal == false))
        {
            return false;
        }
    }
    return true;
}

bool Check(vector<string> & constrain)
{
    for (int i = 0; i < constrain.size(); i++)
    {
        if (CheckString(constrain[i]) == false)
            return false;
    }
    return true;
}

int main()
{
    int N;
    cin >> N;
    vector<string> constrain(N);
    for (int i = 0; i < N; i++)
        cin >> constrain[i];
    int letterCount = CountLetter(constrain);
    int T; 
    cin >> T;
    while (T--)
    {
        memset(data, 0, sizeof(data));
        for (int i = 0; i < letterCount; i++)
        {
            char valuename;
            int value;
            cin >> valuename;
            cin >> value;
            data[valuename - 'A'] = value;
        }
        if (Check(constrain))
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值