Created
June 30, 2013 12:29
-
-
Save yukixz/5894981 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#define MAXINST 65536 // max instructions | |
#define AXISLEN 0x1FFFF | |
bool axis[AXISLEN]; // 2 * 0x10000 - 1 | |
void actU(int left, int right) { | |
// S ← S ∪ T | |
int i; | |
for(i=left; i<=right; i++) | |
axis[i] = 1; | |
} | |
void actI(int left, int right) { | |
// S ← S ∩ T | |
int i; | |
for(i=0; i<left; i++) | |
axis[i] = 0; | |
for(i=right+1; i<AXISLEN; i++) | |
axis[i] = 0; | |
} | |
void actD(int left, int right) { | |
// S ← S − T | |
int i; | |
for(i=left; i<=right; i++) | |
axis[i] = 0; | |
} | |
void actC(int left, int right) { | |
// S ← T − S | |
int i; | |
actI(left, right); | |
for(i=left; i<=right; i++) | |
axis[i] = !axis[i]; | |
} | |
void actS(int left, int right) { | |
// S ← S ⊕ T | |
int i; | |
for(i=left; i<=right; i++) | |
axis[i] = !axis[i]; | |
} | |
int main() | |
{ | |
char act, sign_left, sign_right; | |
int left, right; | |
while( | |
scanf("%c %c%d,%d%c%*c", &act, &sign_left, &left, &right, &sign_right) != EOF | |
) { | |
// Input | |
left *=2; | |
right*=2; | |
if(sign_left =='(') left++; | |
if(sign_right==')') right--; | |
// Process | |
switch(act) { | |
case 'U': | |
actU(left, right); break; | |
case 'I': | |
actI(left, right); break; | |
case 'D': | |
actD(left, right); break; | |
case 'C': | |
actC(left, right); break; | |
case 'S': | |
actS(left, right); break; | |
} | |
} | |
// Output | |
int i; | |
bool isEmpty; | |
isEmpty=1; | |
for(i=0; i<AXISLEN; i++) | |
if(axis[i]) { | |
left=i; // the left 1 | |
i++; // Loop from next one. | |
while(axis[i]) i++; | |
right=i-1; // the Right 1; | |
if(!isEmpty) printf(" "); | |
isEmpty=0; | |
if(left & 1) // odd number | |
printf("(%d,", left/2); // 3 => (1 | |
else // even number | |
printf("[%d,", left/2); // 4 => [2 | |
if(right & 1) | |
printf("%d)", right/2+1); // 3 => 2) | |
else | |
printf("%d]", right/2); // 4 => 2] | |
} | |
if(isEmpty) | |
printf("empty set"); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment