Friday, April 24, 2015

UVa 11988 - Broken Keyboard (a.k.a. Beiju Text)

// UVa 11988 - Broken Keyboard (a.k.a. Beiju Text)

#include <stdio.h>
using namespace std;

struct node {
	char x;
	node *next;
};

char line[100009];

node* empty() {
	node* n = new node;
	n->x = 0;
	n->next = NULL;
	return n;
}

void print(node* n) {
	while (n->next != NULL) {
		if (n->x != 0)
			printf("%c", n->x);
		n = n->next;
	}
}

int main() {
	while (fgets(line, sizeof(line), stdin) != NULL) {
		node* leftBegin = empty();
		node* leftEnd = leftBegin;
		node* rightBegin = empty();
		node* rightEnd = rightBegin;
		for (int i = 0; line[i] != 0; i++) {
			if (line[i] == '[') {
				//right = left + right;
				//left = "";
				leftEnd->next = rightBegin;
				rightBegin = leftBegin;
				leftBegin = empty();
				leftEnd = leftBegin;
			} else if (line[i] == ']') {
				//left = left + right;
				//right = "";
				leftEnd->next = rightBegin;
				leftEnd = rightEnd;
				rightBegin = empty();
				rightEnd = rightBegin;
			} else {
				node* n = new node;
				n->x = line[i];
				n->next = NULL;
				leftEnd->next = n;
				leftEnd = n;
			}
		}

		print(leftBegin);
		print(rightBegin);
		printf("\n");
	}

	return 0;
}

No comments:

Post a Comment