POJ 3295 构造法

本人也不知道构造法到底是什么意思,本题主要思想就是用堆栈来处理。
AC代码:

#include
#include
int stack[1000];
char str[1000];
int top=-1;
int put(char c,int p,int q,int r,int s,int t){
		switch(c){
			case 'p':top++;stack[top]=p;return 1;
			case 'q':top++;stack[top]=q;return 1;
			case 'r':top++;stack[top]=r;return 1;
			case 's':top++;stack[top]=s;return 1;
			case 't':top++;stack[top]=t;return 1;
		}
		return 0;
}
void compute(char c){
	int x,y;
	switch(c){
		case 'K':
			x=stack[top];
			top--;
			y=stack[top];
			stack[top]=(x&&y);
			break;
		case 'A':
			x=stack[top];
			top--;
			y=stack[top];
			stack[top]=(x||y);
			break;
		case 'N':
			x=stack[top];
			stack[top]=(!x);
			break;
		case 'C':
			x=stack[top];
			top--;
			y=stack[top];
			stack[top]=(!x)||y;
			break;
		case 'E':
			x=stack[top];
			top--;
			y=stack[top];
			stack[top]=x==y; 
	}
}
int main()
{
	int p,q,r,s,t;
	int i,j;
	int flag,result;
	int l;
	while(scanf("%s",str)!=EOF){
		flag=1;
		result=1;
		if(str[0]=='0')
			break;
		l=strlen(str);
			for(p=0;p<2;p++){
				for(q=0;q<2;q++){
					for(r=0;r<2;r++){
						for(s=0;s<2;s++){
							for(t=0;t<2;t++)
							{
								for(i=l-1;i>=0;i--)
									if(put(str[i],p,q,r,s,t)==0)
										compute(str[i]);
								result=stack[top];
								if(!result){
									flag=0;
									break;
								}
							}
							if(!flag)
							break;
						}
						if(!flag)
							break;
					}
					if(!flag)
							break;
				}
				if(!flag)
							break;
			}
			if(flag)
            printf("tautology\n");
        else printf("not\n");
        top=-1; 
	}
	return 0; 
 } 

错误代码:(到现在还不知道为什么错…后续还得再看看)

#include
#include
#include
#define bool char 
#define true 1
#define false 0
typedef struct snode{
	bool data;
	struct snode *next;
}LSNode; 
char buf[101];
LSNode *s_stack;
bool data[5]={false};

void init(LSNode **head){
	*head = (LSNode *)malloc(sizeof(LSNode));
	(*head)->next = NULL;
} 
 
int empty(LSNode *head){
	if(head->next == NULL) return 1;
	else return 0;
} 
 
void push(LSNode *head, bool x){
	LSNode *p;
	p = (LSNode *)malloc(sizeof(LSNode));
	p->data = x;
	p->next = head->next;  
	head->next = p;  
}
 
int pop(LSNode *head){
	LSNode *p = head->next;
	if(empty(head)){
		return 0;
	}
	head->next = p->next;    
	free(p);  
	p = NULL;
	return 1;
} 
 
bool top(LSNode *head){
	bool d;
	LSNode *p = head->next;
	if(empty(head)){
		return 0;
	}
	d = p->data; 
	return d;
} 
bool WFF(char* str, int val)
{
	int i;
	int strLen;
	bool a,b;
	//init p q r s t
	for (i=0; i<5; ++i){
		data[i] = (1<<i) & val;
	}
 
	while (!empty(s_stack)) pop(s_stack);
	strLen = strlen(str);
	while (--strLen >= 0)
	{
		switch (buf[strLen])
		{
		case 'p':
			push(s_stack,data[0]);
			break;
		case 'q':
			push(s_stack,data[1]);
			break;
		case 'r':
			push(s_stack,data[2]);
			break;
		case 's':
			push(s_stack,data[3]);
			break;
		case 't':
			push(s_stack,data[4]);
			break;
		case 'K':
			a=top(s_stack); pop(s_stack);
			b=top(s_stack); pop(s_stack);
			push(s_stack,a&&b);
			break;
		case 'A':
			a=top(s_stack); pop(s_stack);
			b=top(s_stack); pop(s_stack);
			push(s_stack,a||b);
			break;
		case 'N':
			a=top(s_stack); pop(s_stack);
			push(s_stack,!a);
			break;
		case 'C':
			a=top(s_stack); pop(s_stack);
			b=top(s_stack); pop(s_stack);
			push(s_stack,!a||b);
			break;
		case 'E':
			a=top(s_stack); pop(s_stack);
			b=top(s_stack); pop(s_stack);
			push(s_stack,a==b);
			break;
		default:
			break;
		}
	}
	return top(s_stack);
}
 
int main()
{
	bool tautology;
	int val;
	while (true)
	{
		init(&s_stack);
		memset(buf, 0, sizeof(char)*101);
		scanf("%s", buf);
		if (strcmp(buf, "0") == 0) break;
		tautology = true;
		for (val=0; val<=0x1f; ++val)
		{
			if (! WFF(buf, val)){
				tautology = false;
				break;
			}
		}
		if (tautology){
			printf("tautology\n");
		}else {
			printf("not\n");
		}
	}
 
	return 0;
}

你可能感兴趣的:(算法,c语言)