2016微软探星夏令营:#1341 : Constraint Checker
来源:互联网 发布:大屏数据 编辑:程序博客网 时间:2024/05/21 06:44
http://hihocoder.com/problemset/problem/1341?sid=827699
#1341 : Constraint Checker
- 样例输入
2A<B<=E3<=E<52A 1B 2E 3A 3B 5E 10
- 样例输出
YesNo
描述
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.
#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;}
- 2016微软探星夏令营:#1341 : Constraint Checker
- 2016微软探星夏令营:#1341 : Constraint Checker
- Constraint Checker 2016微软探星夏令营在线技术笔试
- 2016微软探星夏令营在线技术笔试-#1341 : Constraint Checker
- [HihoCoder]#1341 : Constraint Checker
- 2016微软探星夏令营在线技术笔试
- 2016微软探星夏令营在线技术笔试题解(1)
- 2016微软探星夏令营在线技术笔试题解(2)
- 2016微软探星夏令营在线技术笔试题解(3)
- 2016微软探星夏令营在线技术笔试题解(4)
- 【hihocoder#1341】(微软探星夏令营在线技术笔试第一题)
- 2016微软探星夏令营在线技术笔试-#1342 : Full Binary Tree Picture
- Constraint Checker——hihoCoder176
- [2016 微软预科生计划-探星夏令营在线测试2] Give My Text Back(字符串处理)
- 【在线笔试题解题报告系列】微软在线笔试之 2016微软探星夏令营在线技术笔试(时间:2016.07.17)
- [hihoCoder 1187] Divisors (微软预科生计划-探星夏令营在线测试)
- hihocoder 1534 微软探星夏令营笔试A题 (STL)
- hihocoder 2017微软探星夏令营在线技术笔试 1534 Array Partition
- kvo,kvc
- C/C++笔记(C语言重要问题重讲,内存四区篇)
- 分享一个php时间段切分为指定段的函数
- 《leetcode》:Expression Add Operators
- node.js之Promise初见
- 2016微软探星夏令营:#1341 : Constraint Checker
- 简单handler实现闪屏页面跳转
- 初识Elasticsearch搜索引擎
- [HDU 5408] CRB and Farm (几何+凸包)
- Androida百度地图开发之显示百度地图
- 《leetcode》:Perfect Squares
- linux文本处理三剑客(二):sed命令详解
- Codeforces 696A Lorenzo Von Matterhorn ( LCA )
- 欢迎使用CSDN-markdown编辑器