cbgcbgcbg

Untitled

Feb 21st, 2021
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     ios_base::sync_with_stdio(false);
  6.     cin.tie(NULL);
  7.     string s, t;
  8.     int n, m;
  9.     cin>>n>>m;
  10.     cin>>s>>t;
  11.     int j = 0;
  12.     int ans = 0;
  13.     for (int i = 0; i<m; i++) {
  14.         for (int k = 1; k<=n; k++) {
  15.             ans++;
  16.             if (s[k-1] == t[j]) {
  17.                 j++;
  18.                 if (j == m) {
  19.                     cout<<ans<<endl;
  20.                     return 0;
  21.                 }
  22.             }
  23.            
  24.         }
  25.     }
  26.     cout<<-1<<endl;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment