fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string solve() {
  5. string s, t;
  6. cin >> s >> t;
  7. unordered_set<string> mp;
  8. queue<string> q;
  9. q.push(t);
  10. mp.insert(t);
  11.  
  12. while (!q.empty()) {
  13. string x = q.front(); q.pop();
  14.  
  15. if (x == s) return "Yes";
  16.  
  17. if (x.size() < s.size()) continue;
  18.  
  19. if (x.back() == 'A') {
  20. string tmp = x;
  21. tmp.pop_back();
  22. if (!mp.count(tmp)) {
  23. q.push(tmp);
  24. mp.insert(tmp);
  25. }
  26. }
  27.  
  28. if (x[0] == 'B') {
  29. string tmp = x;
  30. reverse(tmp.begin(), tmp.end());
  31. tmp.pop_back();
  32. if (!mp.count(tmp)) {
  33. q.push(tmp);
  34. mp.insert(tmp);
  35. }
  36. }
  37. }
  38.  
  39. return "No";
  40. }
  41.  
  42. int main() {
  43. ios_base::sync_with_stdio(0);
  44. cin.tie(0);
  45.  
  46. freopen("abba.inp", "r", stdin);
  47. freopen("abba.out", "w", stdout);
  48.  
  49. for (int i = 1; i <= 3; ++i)
  50. cout << solve() << '\n';
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty