fork(9) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. using namespace std;
  5.  
  6. void TEST(bool val, bool answer) {
  7. string s_val = val ? "true" : "false";
  8. string a_val = answer ? "true" : "false";
  9. if(val == answer) {
  10. cout << "PASS!" << endl;
  11. } else {
  12. cout << "FAIL: should be [ " << a_val << " ] but given [ " << s_val << " ]" << endl;
  13. }
  14. }
  15.  
  16. class Iterator {
  17. public:
  18. Iterator(const vector<int>& data) : _data(data) {}
  19. int get_next() { return _data[_pointer++]; }
  20. bool has_next() { return _pointer < _data.size(); }
  21. private:
  22. vector<int> _data;
  23. size_t _pointer{ 0 };
  24. };
  25.  
  26. /////////////////////////////////////////////////////////////////////////////////
  27.  
  28. bool one_dist(Iterator&& a, Iterator&& b) {
  29. bool ins_a = false, ins_b = false, replace = false, diff = false;
  30. char pre_a, pre_b;
  31. while (a.has_next() && b.has_next()) {
  32. int cur_a = a.get_next(), cur_b = b.get_next();
  33. if (!ins_a && !ins_b && !replace) {
  34. if (cur_a != cur_b) {
  35. ins_a = ins_b = replace = diff = true;
  36. }
  37. } else {
  38. if (ins_a && pre_b != cur_a) ins_a = false;
  39. if (ins_b && pre_a != cur_b) ins_b = false;
  40. if (replace && cur_a != cur_b) replace = false;
  41. if (!ins_a && !ins_b && !replace) return false;
  42. }
  43. pre_a = cur_a;
  44. pre_b = cur_b;
  45. }
  46.  
  47. if (!a.has_next() && !b.has_next()) {
  48. return !diff || replace;
  49. } else if (a.has_next()) {
  50. int cur_a = a.get_next();
  51. return (!diff || (ins_a && pre_b == cur_a)) && !a.has_next();
  52. } else if (b.has_next()) {
  53. int cur_b = b.get_next();
  54. return (!diff || (ins_b && pre_a == cur_b)) && !b.has_next();
  55. }
  56. }
  57.  
  58. /////////////////////////////////////////////////////////////////////////////////
  59.  
  60. int main() {
  61. TEST(one_dist(Iterator({1,2}), Iterator({3,1})), false); // 0
  62. TEST(one_dist(Iterator({1,2}), Iterator({2,3})), false); // 0
  63. TEST(one_dist(Iterator({1,2,1}), Iterator({2,1})), true);
  64. TEST(one_dist(Iterator({1,2}), Iterator({2,1,2})), true);
  65. TEST(one_dist(Iterator({1,2,1,2,4}), Iterator({2,1,2,4})), true);
  66. TEST(one_dist(Iterator({1,2,3}), Iterator({1,2,4})), true); // 1
  67. TEST(one_dist(Iterator({1}), Iterator({2,3})), false); // 0
  68. TEST(one_dist(Iterator({1}), Iterator({2,1})), true); // 1
  69. TEST(one_dist(Iterator({2,3}), Iterator({1})), false); // 0
  70. TEST(one_dist(Iterator({1}), Iterator({1,2,3,4,5})), false); // 0
  71. TEST(one_dist(Iterator({1}), Iterator({})), true); // 1
  72. TEST(one_dist(Iterator({}), Iterator({1})), true); // 1
  73. TEST(one_dist(Iterator({1,2,3}), Iterator({2,3})), true); // 1
  74. TEST(one_dist(Iterator({1,3,4}), Iterator({2,3,4})), true); // 1
  75. TEST(one_dist(Iterator({1,2,3}), Iterator({2,4})), false); // 0
  76. TEST(one_dist(Iterator({1,2,3}), Iterator({2})), false); // 0
  77. TEST(one_dist(Iterator({3,4}), Iterator({5})), false); // 0
  78. TEST(one_dist(Iterator({3,4}), Iterator({5,6})), false); // 0
  79. TEST(one_dist(Iterator({1,2,3}), Iterator({1,2,3})), true); // 1
  80. TEST(one_dist(Iterator({}), Iterator({})), true); // 1
  81. TEST(one_dist(Iterator({1}), Iterator({2})), true); // 1
  82. TEST(one_dist(Iterator({1,3,4,5,6,7}), Iterator({1,2})), false); // 0
  83. TEST(one_dist(Iterator({1,3,4,5,6,7}), Iterator({1,4,5,6,7})), true); // 1
  84. TEST(one_dist(Iterator({2,3,4,5,6,7}), Iterator({3,4,5,6,7})), true); // 1
  85.  
  86. return 0;
  87. }
Success #stdin #stdout 0s 3280KB
stdin
Standard input is empty
stdout
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!
PASS!