fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_map>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. int n;
  9. string a, b;
  10. cin >> n >> a >> b;
  11.  
  12. unordered_map<char, int> h1, h2;
  13.  
  14. for (int i = 0; i < n; i++) {
  15. h1[a[i]] = h1[a[i]] + 1;
  16. }
  17.  
  18. for (int i = 0; i < n; i++) {
  19. h2[b[i]] = h2[b[i]] + 1;
  20. }
  21.  
  22. int check = 0;
  23. for (char c = 'a'; c <= 'z'; c++) {
  24. if (h1[c] != h2[c]) {
  25. check = 1;
  26. break;
  27. }
  28. }
  29.  
  30. if (check == 1) {
  31. cout << "Answer not possible." << endl;
  32. } else {
  33. int r = 0;
  34. int i = n - 1;
  35. int j = n - 1;
  36. while (i >= 0 && j >= 0) {
  37. if (a[i] == b[j]) {
  38. r = r + 1;
  39. i = i - 1;
  40. j = j - 1;
  41. } else {
  42. i = i- 1;
  43. }
  44. }
  45. cout << n - r << endl;
  46. }
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 5284KB
stdin
3
"ABD"
"BAD"
stdout
2