Constraint Checker 2016微软探星夏令营在线技术笔试

题意:给你N个串,每个串有不定长表达式, 包含“<=”,"<","A~Z","integer",A-Z的值后面会有T个样例会给你,让你判断N个串是否都是成立的

题解:链表。保存的是这个表达式的类型,是数字还是字母,还有保存判断符,最后暴力从左往右扫就好了,一旦不对就跳出。

描述

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.

样例输入
2
A<B<=E
3<=E<5
2
A 1
B 2
E 3
A 3
B 5
E 10
样例输出
Yes
No 
#pragma comment(linker, "/STACK:102400000,102400000") 
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#include <queue>
#define Pi acos(-1.0)
using namespace std;
typedef long long  ll;
struct ListNode {
	int lei;
	char al;
	ll val;
	int op;
	ListNode *next;
	ListNode():next(NULL){}
};
int n;
ListNode* p[21];
ListNode* rem[21];
ListNode* rrem[21];
char str[22][222222];
map<char,ll>mp;
int fun(){
	for(int i = 0;i < n;i++){
		rrem[i] = rem[i];
		while(rrem[i] != NULL && rrem[i]->next !=NULL){
			ll pre,last;	
			int nowop = rrem[i]->op;
			//printf("op :%d\n", nowop);
			if(rrem[i]->lei == 1) pre = mp[rrem[i]->al];
			else pre = rrem[i]->val;
			
			rrem[i] = rrem[i]->next;
			
			if(rrem[i]->lei == 1) last = mp[rrem[i]->al];
			else last = rrem[i]->val;	
			
			if((nowop == 1 && pre <= last) || (nowop==2 && pre < last)) continue;
			else return 0;
	
		
		}
	}
	
	return 1;
	
}
/*

void test(){
	
	for(int i = 0; i < n;i++){
		rrem[i] = rem[i];
		while(rrem[i]!= NULL){
			if(rrem[i]->lei==1) printf("%d ", mp[rrem[i]->al]);
			else printf("%d ", rrem[i]->val);
			rrem[i] = rrem[i]->next;
		}
		printf("\n");
	}

}*/

int main(){
	mp.clear();
	scanf("%d", &n);
	{
		mp.clear();
		for(int i = 0;i < n;i++){
			p[i] = new ListNode();
			rem[i] = p[i];
		}
		for(int i = 0;i < n;i++){
			scanf("%s", str[i]);
			ll num = 0;
			for(int j = 0;str[i][j]!=0;j++){
				if(str[i][j] >= 'A' && str[i][j] <= 'Z'){
					mp[str[i][j]] = 1;
					p[i]->lei = 1;
					p[i]->al = str[i][j];
				}
				else if(str[i][j] >= '0' && str[i][j] <= '9'){
					num = num * 10 + str[i][j] - '0';
					if(str[i][j+1]==0){
						p[i]->val = num;
						p[i]->lei = 0;
						num = 0;
					}
				}
				else if(str[i][j]=='<'){
					if(str[i][j+1]=='='){
						p[i]->op = 1;
					}
					else{
						p[i]->op = 2;
					}
					if(str[i][j-1] >= '0' && str[i][j-1] <= '9'){
						p[i]->lei = 0;
						p[i]->val = num;
						num = 0;
					}
					p[i]->next = new ListNode();
					p[i] = p[i]->next;
					
				}
			}
		}	
		
		int cnt = 0;
		for(int i = 0; i < 26;i++){
			if(mp[i+'A']) cnt++;
		}
		
		int m;
		scanf("%d", &m);
		int ans[222];
		for(int i = 0;i < m;i++){
			char op[10];
			for(int j = 0;j < cnt;j++){
				ll x;
				scanf("%s%lld", op, &x);
				mp[op[0]] = x;
			}
		//	test();
			ans[i] = fun();
		}
		for(int i = 0;i < m;i++){
			if(ans[i]==1){
				printf("Yes\n");
			}
			else{
				printf("No\n");
			}
		}
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值