2016微软探星夏令营:#1341 : Constraint Checker

来源:互联网 发布:大屏数据 编辑:程序博客网 时间:2024/05/21 06:44

http://hihocoder.com/problemset/problem/1341?sid=827699

#1341 : Constraint Checker

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

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.

样例输入
2A<B<=E3<=E<52A 1B 2E 3A 3B 5E 10
样例输出
YesNo 
没什么难度,就是处理输入输出很麻烦,而且用C++进行数据类型转换比较麻烦

#include <iostream>#include <vector>#include <map>#include <string>using namespace std;int main(){int n, m,len, num = 0;//num 统计未知数个数cin >> n;string s;vector<vector<string>> constraints(n);cin.get();int ascii[26] = { 0 };//存放未知数size_t index;for (int i = 0; i < n; i++){getline(cin, s);while ((index = s.find("<=")) != string::npos)//"<="->"^"s.replace(index, 2, "^");len = s.length();for (int j = 0; j < len;j++){char c = s[j];string number = "",ss = "";if (c == '<' || c == '^'){ss += c;constraints[i].push_back(ss);}else if (c >= 'A'&& c <= 'Z'){ss += c;constraints[i].push_back(ss);if (ascii[c - 'A'] == 0)num++;ascii[c - 'A'] = 1;}else//数字{while (j < len&&s[j] >= '0'&&s[j] <= '9')number+=s[j++];j--;constraints[i].push_back(number);}}}cin >> m;string c1;int c2;int a, b;bool yes;for (int i = 0; i < m; i++){yes = true;map<string, int> table;for (int j = 0; j < num; j++)//未知数取值的键值对,如A:3{cin >> c1 >> c2;table[c1] = c2;}for (int j = 0; j < n; j++){vector<string> vec = constraints[j];len = vec.size()-2;for (int k = 0; k < len; k += 2){a = (vec[k] >= "A"&&vec[k] <= "Z") ? table[vec[k]] : atoi(vec[k].c_str());b = (vec[k+2] >= "A"&&vec[k+2] <= "Z") ? table[vec[k+2]] : atoi(vec[k+2].c_str());yes = vec[k + 1] == "<" ? a < b : a <= b;//判断不等式if (!yes)break;}if (!yes)break;}s = yes ? "Yes" : "No";cout << s << endl;}return 0;}


0 0
原创粉丝点击