Codeforces Round #306 (Div. 2) 550A - Two Substrings Solution

                               

         
Problem Link: https://codeforces.com/problemset/problem/550/A
Solution in C++:
  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. /// Abul Hasnat Tonmoy
  4. /// Department of CSE,23rd batch
  5. /// Islamic University,Bangladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. int main() {
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0);
  12. string s;
  13. cin >> s;
  14. int i;
  15. int l = s.size();
  16. bool a = false, b = false, c = false, d = false;
  17. for (int i = 0; i < l - 1; i++) {
  18. if ((s[i] == 'B') && (s[i + 1] == 'A') && (s[i + 2] == 'B') &&
  19. (d == false) && (c == false))
  20. d = true, i += 2;
  21. else if ((s[i] == 'A') && (s[i + 1] == 'B') && (s[i + 2] == 'A') &&
  22. (c == false) && (d == false))
  23. c = true, i += 2;
  24. else if ((s[i] == 'A') && (s[i + 1] == 'B') && (a == false))
  25. a = true, i++;
  26. else if ((s[i] == 'B') && (s[i + 1] == 'A') && (b == false))
  27. b = true, i++;
  28. }
  29. if ((a == true && b == true) ||
  30. ((c == true || d == true) && (a == true || b == true)))
  31. cout << "YES" << endl;
  32. else
  33. cout << "NO" << endl;
  34. }

Another solutoin is given bellow :

  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. /// Abul Hasnat Tonmoy
  4. /// Department of CSE,23rd batch
  5. /// Islamic University,Bangladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. int main() {
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0);
  12. char s[100100], *p;
  13. cin >> s;
  14. if ((p = strstr(s, "AB")) && strstr(p + 2, "BA"))
  15. cout << "YES" << endl;
  16. else if ((p = strstr(s, "BA")) && strstr(p + 2, "AB"))
  17. cout << "YES" << endl;
  18. else
  19. cout << "NO" << endl;
  20. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.