fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int INF = 1e9;
  5. int nxt[1005][26], dp[1005][1005];
  6.  
  7. int main() {
  8. ios::sync_with_stdio(false);
  9. cin.tie(NULL);
  10.  
  11. string a, b;
  12. cin >> a >> b;
  13.  
  14. int n = a.length();
  15. int m = b.length();
  16.  
  17. for (int c = 0; c < 26; ++c) nxt[m][c] = -1;
  18. for (int i = m - 1; i >= 0; --i) {
  19. for (int c = 0; c < 26; ++c) nxt[i][c] = nxt[i + 1][c];
  20. nxt[i][b[i] - 'a'] = i;
  21. }
  22.  
  23. for (int j = 0; j <= m; ++j) dp[n][j] = INF;
  24.  
  25. for (int i = n - 1; i >= 0; --i) {
  26. int char_idx = a[i] - 'a';
  27. for (int j = 0; j <= m; ++j) {
  28. int option1 = dp[i + 1][j];
  29. int pos = (j < m) ? nxt[j][char_idx] : -1;
  30. int option2;
  31.  
  32. if (pos == -1) {
  33. option2 = 1;
  34. } else {
  35. option2 = 1 + dp[i + 1][pos + 1];
  36. }
  37.  
  38. dp[i][j] = min(option1, option2);
  39. }
  40. }
  41.  
  42. cout << dp[0][0];
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
1000000000