This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define rip(i,a,b) for(ll i=a;i<=b;i++)
#define in insert
using namespace std;
const ll M=998244353;
int32_t main(){
//ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n,m;
cin>>n>>m;
string s1,s2;
cin>>s1>>s2;
s1='!'+s1;
s2='!'+s2;
int idx=0;
for(int i=1;i<=n;i++){
if(s1[i]==s2[1]) idx=i;
}
if(idx==0){
cout<<-1;
return 0;
}
int ans=0;
for(int i=2;i<=m;i++){
bool c=0;
for(int j=1;j<=n;j++){
if(s1[j]==s2[i]){ans+=abs(j-idx);idx=j;c=1;}
}
if(!c){
cout<<-1;
return 0;
}
}
cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |