AhmedAshraff

Untitled

Oct 18th, 2025
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. #define boAshraf { ios_base::sync_with_stdio(false); cin.tie(NULL); }
  4. #define ll long long
  5. #define sz(s) (int)(s).size()
  6. #define endl "\n"
  7. #define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
  8. #include <ext/pb_ds/assoc_container.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10. using namespace __gnu_pbds;
  11. using namespace std;
  12.  
  13. void File();
  14. void sol();
  15. int  f[(int)1e5+5];
  16. void KMP(const string&pat){
  17.     int len = 0;
  18.     f[0] = 0;
  19.     for (int i = 1; i < pat.size(); ++i) {
  20.         while (len>0 && pat[i] != pat[len])
  21.             len = f[len - 1];
  22.         if (pat[i] == pat[len])
  23.             ++len;
  24.         f[i] = len;
  25.     }
  26. }
  27. vector<vector<int>>aut;
  28. void compute_automaton(string s) {
  29.     s += '#';
  30.     int n = s.size();
  31.     aut.assign(n, vector<int>(27));
  32.     for (int i = 0; i < n; i++) {
  33.         for (int c = 0; c < 26; c++) {
  34.             if (i > 0 && 'a' + c != s[i])
  35.                 aut[i][c] = aut[f[i - 1]][c];
  36.             else
  37.                 aut[i][c] = i + ('a' + c == s[i]);
  38.             aut[i][26]=max(aut[i][c],aut[i][26]);
  39.         }
  40.     }
  41. }
  42. string pat,s;
  43. const int N=1e4,M=1e3;
  44. int n,m;
  45. int idd;
  46. int dp[N][M];
  47. int vis[N][M];
  48. int rec(int i,int match){
  49.     if(match==m)return 1e9;
  50.     if(i==n)return 0;
  51.     int &ret=dp[i][match];
  52.     if(vis[i][match]==idd)return ret;
  53.     vis[i][match]=idd;
  54.     ret=1e9;
  55.     if(s[i]==pat[match]){
  56.         ret=min(rec(i+1,match)+1,rec(i+1,match+1));
  57.     }
  58.     int sz=aut[match][s[i]-'a'];
  59.     ret= min(ret,rec(i+1,sz));
  60.     return ret;
  61. }
  62. int main() {
  63.     boAshraf
  64.     File();
  65.     int t = 1;
  66.     //cin >> t;
  67.     while (t--) {
  68.         sol();
  69.     }
  70.     return 0;
  71. }
  72. void sol() {
  73.     while(cin>>s>>pat){
  74.         KMP(pat);
  75.         compute_automaton(pat);
  76.         n=s.size();m=pat.size();
  77.         ++idd;
  78.         cout<<rec(0,0)<<endl;
  79.     }
  80. }
  81.  
  82. void File() {
  83. #ifndef ONLINE_JUDGE
  84.     freopen("input.txt", "r", stdin);
  85.     freopen("output.txt", "w", stdout);
  86. #endif
  87. }
Advertisement
Add Comment
Please, Sign In to add comment