Cod sursa(job #2278199)

Utilizator andrici_cezarAndrici Cezar andrici_cezar Data 7 noiembrie 2018 14:16:52
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <cstdio>
#include <cstring>
#include <stdlib.h>
 
char SO[1001], S[1001], schimb[101];
long n, LIT[28], i;
 
bool replace(char S[1001], const char ce[10], const char cu[10]) {
	char *p;
	char t[1005];
	p = strstr(S, ce);
	bool ok = false;

	while (p != 0) {
		strcpy(t, p);
		strcpy(p, cu);
		strcpy(p+strlen(cu), t+strlen(ce));
		p = strstr(p, ce);
		ok = true;
	}
 
	return ok;
}
 
void erase(char S[1001], const char ce[10]) {
	
	char *p;
	p = strstr(S, ce);
 
	while (p != 0) {
		strcpy(p, p+strlen(ce));
		p = strstr(p, ce);
	}
 
}
 
int rez(char S[1001]) {
	long t, i;
	char x[5] = "", y[5]="";

	for (i = 0; i <= 27; ++i) {
		x[0] = (char)(i+'A');
		y[0] = (char)(LIT[i]+'0');
		replace(S, x, y);
	}

	while ((t=strlen(S)) != 1) {
		replace(S, "(0)", "0") ||
		replace(S, "(1)", "1") ||
		replace(S, "!0", "1") ||
		replace(S, "!1", "0") ||
 
		replace(S, "0&0", "0") ||
		replace(S, "0&1", "0") ||
		replace(S, "1&1", "1") ||
		replace(S, "1&0", "0") ||

		replace(S, "0|0", "0") ||
		replace(S, "0|1", "1") ||
		replace(S, "1|1", "1") ||
		replace(S, "1|0", "1");
	}

	return S[0] - '0';
}
 
int main()
{
	freopen("bool.in", "r", stdin);
	freopen("bool.out", "w", stdout);
 
		fgets(SO, 1000, stdin);
		SO[strlen(SO)-1] = '\0';

		scanf("%ld\n", &n);
		scanf("%s\n", schimb);
 
		replace(SO, "NOT", "!");
		replace(SO, "NOT", "!");
		replace(SO, "AND", "&");
		replace(SO, "OR", "|");
		replace(SO, "TRUE", "1");
		replace(SO, "FALSE", "0");
		erase(SO, " ");
 
		for (i = 0; i < n; ++i) {
			LIT[ schimb[i] - 'A' ] = 1 - LIT[ schimb[i] - 'A' ];
			strcpy(S, SO);
			printf("%d", rez(SO));
			strcpy(SO, S);
		}
		printf("\n");
	return 0;
}