PrefPref

QUESTION

You are given two string S and T. Find the maximal length of some prefix of the string S which occurs in strings T as subsequence.\n\nInput\nThe first line contains string S. The second line contains string T. Both strings consist of lowecase Latin letters.\n\nOutput\nOutput one integer – answer to the question.\n\nConstraints\n1 <= length of S, T <= 10^6.

ANSWER

#include<bits/stdc++.h>
using namespace std;
int main(){
 
		string s1,s2;
cin>>s1>>s2;
int count = 0 ;
int pos = -1;
int k = s2.length();
for(int i=0;i<s1.length();i++)
{
	
	pos = s2.find(s1[i],pos+1);
	if(pos!=string::npos)
		count ++;
	else
        break;
}
cout<<count;
 
 
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock