-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplace With First
66 lines (60 loc) Β· 1.26 KB
/
Replace With First
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
bool checkPossible(string s1, string s2) {
int fromLeft = 0;
int fromRight = 0;
for(int i=0;i<s1.length() && i < s2.length();i++) {
if(s1[i] == s2[i]) {
fromLeft++;
}
else {
break;
}
}
int j = s1.length()-1;
for(int i=s2.length()-1;i>=0; i--) {
if(s1[j] == s2[i]) {
fromRight++;
}
else {
break;
}
j--;
}
if(fromLeft+fromRight >= s2.length()) {
return true;
}
return false;
}
int main() {
int t;
cin>>t;
int len1, len2;
string s1, s2;
while(t--) {
cin>>len1>>len2;
cin>>s1>>s2;
if(s1[0] != s2[0]) {
cout<<-1<<endl;
continue;
}
if(s1 == s2) {
cout<<0<<endl;
continue;
}
if(s1.length() > s2.length()) {
if(checkPossible(s1,s2)) {
cout<<1<<endl;
continue;
}
}
else if(s2.length() > s1.length()) {
if(checkPossible(s2, s1)) {
cout<<1<<endl;
continue;
}
}
cout<<2<<endl;
}
return 0;
}