#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;
if(idx+1<=m && s1[idx+1]==s2[i]){
ans++;
c=1;
idx++;
}
else if(idx-1>=1 && s1[idx-1]==s2[i]){
ans++;
c=1;
idx--;
}
if(!c){
cout<<-1;
return 0;
}
}
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
304 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |