Salvens

F

Aug 7th, 2023
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. #define int long long
  7.  
  8. const long long INF = 1e9 + 7;
  9. const int MAXN = 200 + 10;
  10. const int N = 1e5 + 10;
  11.  
  12. int Sum(int a1, int n, int d) {
  13.     return (2 * a1 + d * (n - 1)) * n / 2;
  14. }
  15.  
  16. signed main() {
  17.     ios_base::sync_with_stdio(false);
  18.     cin.tie(nullptr);
  19.     cout.tie(nullptr);
  20.  
  21.     int n, m;
  22.     cin >> n >> m;
  23.     string a, b;
  24.     cin >> a >> b;
  25.     map<int, int> cnt, kol;
  26.     for (int i = 0; i < m; ++i) {
  27.         ++cnt[b[i]];
  28.     }
  29.     int ans = 0, l = 0;
  30.     for (int r = 0; r < n; ++r) {
  31.         ++kol[a[r]];
  32.         while (kol[a[r]] > cnt[a[r]] && l <= r) {
  33.             --kol[a[l]];
  34.             ++l;
  35.         }
  36.         ans += r - l + 1;
  37.     }
  38.     cout << ans << '\n';
  39. }
Advertisement
Add Comment
Please, Sign In to add comment