Hello today I came across this:
Given 2 strings a and b find the indices of the characters in A to be changed so that it becomes a substring of B, and its guaranteed to have an answer.
I had a few mistakes implementing it then I figured out the fix to my mistakes in which i don't understand so I hope someone helps me understand them.
first here are a few sample inputs:
input
3 5
abc
xaybz
output
2
2 3
---------
input
4 10
abcd
ebceabazcd
output
1
2 |
First 2 integers in the input are the sizes of A and B. The output is basically the 1 index-ed part of string A that needs to be changed in order to be a substring of B.
my implementation(after fixing it):
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
|
#include <bits/stdc++.h>
#define fl(i,n) for(int i = 0; i < n; i++)
#define ll long long
#define nl endl
#define pb push_back
#define mp make_pair
#define PII pair<int,int>
#define EPS 1e-9
#define INF 1e16
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
string s, t;
cin >> s >> t;
int pos;
vector<int> c, ans;
ans.resize(10000);
for(int i = 0; i <= m-n; i++){
c.clear();
pos = 0;
for(int j = 0; j < n; j++){
if(t[j+i] != s[j]) {
pos++;
c.pb(j);
}
}
if(ans.size() > pos){
ans = c;
}
}
cout << ans.size() << nl;
for(auto x: ans) cout << x+1 << " ";
return 0;
}
|
Here are the parts I don't get:
1. The outer loop condition:
for(int i = 0; i <= m-n; i++)
Why is it m-n ? why not n or why not m? I got a wrong answer on more than one of the testcases due to having the loop set to i < m instead of i <= m-n.
2. The if condition in the middle:
if(t[j+i] != s[j])
What does "j+i" represent?
Thanks!