fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Node {
  5. public:
  6. int val, size = 0;
  7. Node* next;
  8. Node(int v) {
  9. val = v;
  10. next = nullptr;
  11. }
  12.  
  13. void insert(int v) {
  14. size++;
  15. Node* nw = new Node(v);
  16. Node* t = this;
  17. while (t->next != nullptr) {
  18. t = t->next;
  19. }
  20. t->next = nw;
  21. }
  22.  
  23. int search(int s) {
  24. int ans = -1, i = 0;
  25. Node* t = this->next;
  26. while (t->next != nullptr) {
  27. // Debug Purposes
  28. // cerr << t->val << " " << i << " " << s << "\n";
  29. if(t->val == s) break;
  30. t = t->next;
  31. i++;
  32. }
  33. if(t->val == s) ans = i;
  34. return ans;
  35. }
  36.  
  37. bool isSame(Node *v) {
  38. bool ans = true;
  39. Node* a = this;
  40. Node* b = v;
  41. if(v->size == this->size) {
  42. do {
  43. a = a->next;
  44. b = b->next;
  45. ans &= a->val == b->val;
  46. } while(a->next != nullptr);
  47. } else ans = false;
  48.  
  49. return ans;
  50. }
  51.  
  52. void print() {
  53. Node* t = next;
  54. while(t->next != nullptr) {
  55. cout << t->val << " ";
  56. t = t->next;
  57. }
  58. cout << t->val << "\n";
  59. }
  60. };
  61.  
  62. int main() {
  63. Node *a = new Node(0);
  64. int x;
  65. while(cin >> x) {
  66. if(x == -1) break;
  67. a->insert(x);
  68. }
  69. Node *b = new Node(0);
  70. while(cin >> x) {
  71. if(x == -1) break;
  72. b->insert(x);
  73. }
  74.  
  75. cout << (a->isSame(b) ? "YES\n" : "NO\n");
  76. }
Success #stdin #stdout 0.01s 5292KB
stdin
10 20 30 40 50 -1
10 20 30 40 50 -1
stdout
YES