NAME2: RUNTIME ERROR,PLZ HELP!!

I am getting runtime error in this code , but it is working fine on my machine. Please help!!!

The link to problem is -http://www.codechef.com/problems/NAME2.

#include
#include <string.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int i,j,flag;
char a[26000];
char b[26000];
cin>>a;
cin>>b;
cout<<a<<endl;
cout<<b<<endl;
flag = 0;
if( strlen(a) < strlen(b)){
i = 0;

		for(j =0; j < strlen(b); j++)
		{
			if(a[i] == b[j])
			{
				i++;
			}
			if(i  == strlen(a))
			{
				flag = 1;
				break;
			}
		}
	}
	else if (strlen(a) > strlen(b)){
		j = 0;
			for(j =0; j < strlen(a); j++)
		{
			if(a[j] == b[i])
			{
				i++;
			}
			if(i  == strlen(b))
			{
				flag = 1;
				break;
			}
		}
	}
	else 
	{
		if(strstr(a,b))
		{
			flag = 1;
		}
		else {
			flag = 0;
		}
	}
	if(flag == 1)
	{
		cout<<"YES"<<endl;
	}
	else {
		cout<<"NO"<<endl;
	}
}
return 0;

}

In the loop,

**else if (strlen(a) > strlen(b))
**
you are assigning j=0; but it should be i=0;
try this and see if the code works

Also, why are you calculating the values strlen(a) and strlen(b), inside the loops again and again.

Calculate them once,i.e., after taking input for the strings a[] and b[] and store them in some variables, say l1 and l2.

This is important because,

For example,

In the loop for(j =0; j < strlen(a); j++)

strlen(a) is being called everytime the condition j< strlen(a) is being checked, which is strlen(a) times, when the loop executes without any break being encountered.

Note: This can cause time limit exceed, if the constraints are tight.

2 Likes

thanks sir your answer was very helpful.

//