Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- char cn[96];
- char sig[96];
- int toint(char c) {
- if (c <= '9')
- return c - '0';
- else
- return 10 + (c - 'A');
- }
- void sw(int *x, int *y, int *i) {
- (*i)++;
- int c = toint(cn[*i]);
- (*i) += 2;
- int c2 = cn[*i];
- if (c2 == 'x')
- y = x;
- (*i)++;
- c2 = toint(cn[*i]);
- (*i) += 2;
- int war = toint(cn[*i]);
- int currentbit = ((*y) >> c2) & 1;
- if (currentbit == war) {
- (*x) ^= 1UL << c;
- }
- }
- void sg(int *x, int *res, int *i, int bit) {
- (*i)++;
- int c = toint(sig[*i]);
- int currentbit = ((*x) >> c) & 1;
- (*i)--;
- if (currentbit == 1) {
- *res = ((1 << bit) | (*res));
- }
- }
- void dsg(int *x, int *res, int *i, int bit) {
- (*i)++;
- int c = toint(sig[*i]);
- int currentbit = ((*res) >> bit) & 1;
- (*i)--;
- if (currentbit == 1) {
- *x = ((1 << c) | (*x));
- }
- }
- int main(void) {
- char what[16];
- scanf("%s", what);
- int n = 0;
- char c;
- while ((c = getchar()) != ']') {
- sig[n++] = c;
- }
- for (int i = 0; i < 4; i++)
- getchar(); // _bin
- int l = 0;
- if (getchar() != '\n') {
- while ((c = getchar()) != '\n') {
- cn[l++] = c;
- }
- }
- if (!strcmp(what, "encode")) {
- for (int i = 0; i < 7; i++)
- getchar();
- int x, y, res = 0;
- scanf("%d", &y);
- getchar();
- scanf("%d", &x);
- for (int i = 0; i < l; i++) {
- int c = cn[i];
- if (c == 'x') {
- sw(&x, &y, &i);
- } else if (c == 'y') {
- sw(&y, &x, &i);
- }
- }
- int bit = 0;
- for (int i = n - 1; i > 0; i--) {
- int c = sig[i];
- if (c == 'y') {
- sg(&y, &res, &i, bit);
- bit++;
- } else if (c == 'x') {
- sg(&x, &res, &i, bit);
- bit++;
- }
- }
- printf("%d\n", res);
- return 0;
- } else {
- // decode
- int res;
- scanf("%d", &res);
- int x = 0, y = 0;
- int bit = 0;
- for (int i = n - 1; i > 0; i--) {
- int c = sig[i];
- if (c == 'y') {
- dsg(&y, &res, &i, bit);
- bit++;
- } else if (c == 'x') {
- dsg(&x, &res, &i, bit);
- bit++;
- }
- }
- for (int i = 0; i < l; i++) {
- int c = cn[i];
- if (c == 'x') {
- sw(&x, &y, &i);
- } else if (c == 'y') {
- sw(&y, &x, &i);
- }
- }
- printf("(y,x)=(%d,%d)\n", y, x);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement