fork download
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4. using namespace std;
  5.  
  6. bool BSC(string S1, string S2) {
  7. stack<char> stack1, stack2;
  8.  
  9. // Processing S1
  10. for (char c : S1) {
  11. if (c != '#') {
  12. stack1.push(c);
  13. } else if (!stack1.empty()) {
  14. stack1.pop();
  15. }
  16. }
  17.  
  18. // Processing S2
  19. for (char c : S2) {
  20. if (c != '#') {
  21. stack2.push(c);
  22. } else if (!stack2.empty()) {
  23. stack2.pop();
  24. }
  25. }
  26.  
  27. // Comparing the contents of the stacks
  28. if (stack1.size() != stack2.size()) {
  29. return false;
  30. }
  31.  
  32. while (!stack1.empty()) {
  33. if (stack1.top() != stack2.top()) {
  34. return false;
  35. }
  36. stack1.pop();
  37. stack2.pop();
  38. }
  39.  
  40. return true;
  41. }
  42.  
  43. int main() {
  44. string S1 = "abcdeg#f", S2 = "ad#bcg#def";
  45.  
  46. cout << "Original string S1: " << S1 << endl;
  47. cout << "Original string S2: " << S2 << endl;
  48.  
  49. if (BSC(S1, S2)) {
  50. cout << "S1 and S2 are equal after processing backspaces." << endl;
  51. } else {
  52. cout << "S1 and S2 are not equal after processing backspaces." << endl;
  53. }
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
Original string S1: abcdeg#f
Original string S2: ad#bcg#def
S1 and S2 are equal after processing backspaces.