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

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

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

描述

Given a set of constraints like 0 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 样例输出 
  
Yes
No 
#pragma comment(linker, "/STACK:102400000,102400000") 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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];
mapmp;
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");
			}
		}
		
	}
}

你可能感兴趣的:(ACM,acm)