#include #include #include #include #include using namespace std; struct coord_t { int x, y, z; coord_t(const coord_t &o) { x = o.x; y = o.y; z = o.z; } coord_t(int _x = 0, int _y = 0, int _z = 0): x(_x), y(_y), z(_z) {} bool operator<(const coord_t &o) const { if (x != o.x) { return x < o.x; } if (y != o.y) { return y < o.y; } return z < o.z; } bool operator==(const coord_t &o) const { return (x == o.x && y == o.y && z == o.z); } }; struct bar_t { coord_t p[2]; bar_t(const coord_t &p0, const coord_t &p1) { if (p0 < p1) { p[0] = p0; p[1] = p1; } else { p[0] = p1; p[1] = p0; } } bool operator<(const bar_t &o) const { if (p[0] < o.p[0]) { return true; } if (o.p[0] < p[0]) { return false; } return p[1] < o.p[1]; } bool operator==(const bar_t &o) const { return (p[0] == o.p[0] && p[1] == o.p[1]); } }; void input_key(set &key, int n) { coord_t p; string elem; pair labels[64]; for (int i = 0; i < 64; i++) { labels[i].first = false; } for (int i = 0; i < n; i++) { coord_t np(p); cin >> elem; if ('0' <= elem[0] && elem[0] <= '9') { int label; istringstream iss(elem); iss >> label; if (labels[label].first) { np = labels[label].second; } else { labels[label].first = true; labels[label].second = p; } } else if (elem[0] == '+' || elem[0] == '-') { if (elem[1] == 'x') { np.x += (elem[0] == '+' ? +1 : -1); } else if (elem[1] == 'y') { np.y += (elem[0] == '+' ? +1 : -1); } else if (elem[1] == 'z') { np.z += (elem[0] == '+' ? +1 : -1); } key.insert(bar_t(p, np)); } p = np; } } void normalize(set &key) { set nkey; coord_t offset(INT_MAX, INT_MAX, INT_MAX); for (set::iterator p = key.begin(); p != key.end(); p++) { if (offset.x > p->p[0].x) { offset.x = p->p[0].x; } if (offset.y > p->p[0].y) { offset.y = p->p[0].y; } if (offset.z > p->p[0].z) { offset.z = p->p[0].z; } } for (set::iterator p = key.begin(); p != key.end(); p++) { nkey.insert(bar_t(coord_t(p->p[0].x - offset.x, p->p[0].y - offset.y, p->p[0].z - offset.z), coord_t(p->p[1].x - offset.x, p->p[1].y - offset.y, p->p[1].z - offset.z))); } key = nkey; } void rotate_x(set &key) { set nkey; for (set::iterator p = key.begin(); p != key.end(); p++) { nkey.insert(bar_t(coord_t(p->p[0].x, p->p[0].z, -p->p[0].y), coord_t(p->p[1].x, p->p[1].z, -p->p[1].y))); } key = nkey; } void rotate_y(set &key) { set nkey; for (set::iterator p = key.begin(); p != key.end(); p++) { nkey.insert(bar_t(coord_t(p->p[0].z, p->p[0].y, -p->p[0].x), coord_t(p->p[1].z, p->p[1].y, -p->p[1].x))); } key = nkey; } void rotate_z(set &key) { set nkey; for (set::iterator p = key.begin(); p != key.end(); p++) { nkey.insert(bar_t(coord_t(p->p[0].y, -p->p[0].x, p->p[0].z), coord_t(p->p[1].y, -p->p[1].x, p->p[1].z))); } key = nkey; } int main() { int n; while (cin >> n && n > 0) { set ka, kb; input_key(ka, n); normalize(ka); cin >> n; input_key(kb, n); bool matched = false; for (int i = 0; i < 4; i++) { rotate_x(kb); normalize(kb); for (int j = 0; j < 4; j++) { rotate_y(kb); normalize(kb); for (int k = 0; k < 4; k++) { rotate_z(kb); normalize(kb); if (ka == kb) { matched = true; break; } } if (matched) { break; } } if (matched) { break; } } if (matched) { cout << "SAME" << endl; } else { cout << "DIFFERENT" << endl; } } }