< Back to forum

EDIT-DISTANCE SPOJ

PROBLEN LINK ->https://www.spoj.com/problems/EDIST/

My code gets terminated without running over all tes cases i.g when input string size is small... all test cases are done....but on giving string ssize of 20-25... the code gets terminated only after 2-3 test cases. When i submitted it on SPOJ...it gave runtime error SIGSEGV.

MY CODE :-

#include<bits/stdc++.h>
using namespace std;
int main()
{
	 long long int temp;
	 cin>>temp;
	 while(temp--)
	 {
	 	char a[2002];char b[2002];
	 	scanf("%s",a);scanf("%s",b);
	/* 	if(strlen(a)==0||strlen(b)==0)
	 	{
	 		cout<<abs(strlen(a)-strlen(b))<<"\n";
	 		continue;
		} */
	 	long long int r=(strlen(a)+1),s=(strlen(b)+1);
		 long long int h[r][s],i,j;
	 	for(i=0;i<r+1;i++)
	 	h[i][0]=i;
	 	for(i=0;i<s+1;i++)
	 	h[0][i]=i;
	 	for(i=1;i<r+1;i++)
	 	{
	 		for(j=1;j<s+1;j++)
	 		{
	 			if(a[i-1]==b[j-1])
	 			h[i][j]=h[i-1][j-1];
	 			else
	 			h[i][j]=(min(min(h[i-1][j],h[i][j-1]),h[i-1][j-1])+1);
			}
	    }
	 cout<<h[r-1][s-1]<<"\n";
    }
	 return 0;
}

 

Asked by: ABHISHEK_KUMAR2 on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar

you code is getting a segmentation fault, since you are accessing h[r][s] but array h is accesssible only upto h[r-1][s-1].
change this :

 long long int h[r][s],i,j;

to 

 long long int h[r+1][s+1],i,j;
Shubham_Kumar_Gupta last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.