fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. const int M = 1e9 + 7;
  5.  
  6. // string +dp 10th sep Amazon OA
  7.  
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(NULL);
  12. string s, t;
  13. cin >> s >> t;
  14. int m = s.size();
  15. int n = t.size();
  16. vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
  17. for (int i = m - 1; i >= 0; i--)
  18. {
  19. dp[i][n] = dp[i + 1][n] + pow(2, m - i - 1);
  20. }
  21. for (int i = m - 1; i >= 0; i--)
  22. {
  23. for (int j = n - 1; j >= 0; j--)
  24. {
  25. ll skip_count = dp[i + 1][j];
  26. ll count = 0;
  27. if (s[i] > t[j])
  28. {
  29. count += pow(2, m - i - 1);
  30. }
  31. else if (s[i] == t[j])
  32. {
  33. count += dp[i + 1][j + 1];
  34. }
  35. dp[i][j] = count + skip_count;
  36. }
  37. }
  38.  
  39. cout << dp[0][0] << endl;
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5320KB
stdin
cbeafg cba
stdout
36