Loading

Paste #pka5bjh2j

  1. #include <string>
  2. #include <algorithm>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. // it's NOT FAIR
  8. // I submitted my solution before you changed the task
  9.  
  10. int K;
  11.  
  12. /*
  13. A -> aAd | abBcd
  14. B -> bBc | _
  15. */
  16.  
  17. // generate strings that start with exactly `depth` "a" chars
  18. // there is only one, if any
  19. void B(int depth)
  20. {
  21.     // in spite of what the grammar above says,
  22.     // I won't call myself recursively
  23.     // because grammar doesn't and can't take
  24.     // the desired length into account
  25.  
  26.     // we must add at least one b/c pair
  27.     if ((depth + 1) * 2 > K) {
  28.         // not a chance
  29.         return;
  30.     }
  31.     cout << string(depth, 'a');
  32.     cout << string(K/2 - depth, 'b');
  33.     cout << string(K/2 - depth, 'c');
  34.     cout << string(depth, 'd');
  35.     cout << endl;
  36. }
  37.  
  38. // generate strings that start with `depth` or more "a" chars
  39. void A(int depth)
  40. {
  41.     if (depth * 2 >= K) {
  42.         // makes no sense to dig further
  43.         return;
  44.     }
  45.     // note: sorting "a" before "b"
  46.     A(depth + 1); // > depth
  47.     B(depth + 1); // == depth
  48. }
  49.  
  50. using namespace std;
  51. int main()
  52. {
  53.     cin >> K;
  54.     if (K & 1) return 0;
  55.     A(0);
  56. }