Advertisement
pb_jiang

LC332T4

Feb 11th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minimumScore(string s, string t) {
  4.         int sn = s.size(), tn = t.size();
  5.         vector<int> pref(sn), post(sn);
  6.        
  7.         pref[0] = s[0] == t[0] ? 1 : 0;
  8.         for (int i = 1; i < sn; ++i) {
  9.             if (pref[i-1] < tn)
  10.                 pref[i] = s[i] == t[pref[i-1]] ? pref[i-1] + 1 : pref[i-1];
  11.             else
  12.                 pref[i] = tn;
  13.         }
  14.            
  15.         post[sn-1] = s[sn-1] == t[tn-1] ? 1 : 0;
  16.         for (int i = sn - 2; i >= 0; --i) {
  17.             if (post[i+1] < tn)
  18.                 post[i] = s[i] == t[tn - 1 - post[i+1]] ? post[i+1] + 1 : post[i+1];
  19.             else
  20.                 post[i] = tn;
  21.         }
  22.        
  23.         int ret = INT_MAX;
  24.         for (int i = 0; i + 1 < sn; ++i) ret = min(ret, max(tn - (pref[i] + post[i + 1]), 0));
  25.         ret = min(ret, tn - pref[sn - 1]);
  26.         ret = min(ret, tn - post[0]);
  27.         return ret;
  28.     }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement