Substring Atcoder

Substring, basically in this question we have to change main string (S) so that substring (T) can fall into it. We will apply naive approach of pattern searching & whenever there is mismatch between two letter we will increase count (c) , now when we come out of loop we will check whether this count was minimum. If it is so then update minimum.

 

Source Code 

#include<bits/stdc++.h>
using namespace std;

#define rep(a,b) for(ll ii=a;ii<b;ii++)
#define ll long long int
#define test() int tt; cin>>tt; while(tt--) 
#define fst_io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define lg 1000000000

int main()
{
  int i, j, min = INT_MAX, c;	
  string s, ss;
  cin>>s>>ss;

  for(i = 0; i<=s.length()-ss.length(); i++)
  {
  	for(j = 0; j<ss.length(); j++)
  	{
  	  if(s[i+j] != ss[j]) c++;	
  	}
  	if(c<min) min = c;
  	c = 0;
  }
  cout<<min<<endl;
  return 0;
}

No comments:

If you have any doubt or suggestion let me know in comment section.

Powered by Blogger.