Have you consider about empty lines?i think the problem is with my way of reading input cos i ran a lot of test cases and my algo seems fine...
Input :
Code: Select all
a
a
Code: Select all
0
0
Hope it helps

Anggakusuma
Moderator: Board moderators
Have you consider about empty lines?i think the problem is with my way of reading input cos i ran a lot of test cases and my algo seems fine...
Code: Select all
a
a
Code: Select all
0
0
I'm suspicious with this fragment:paulidirac wrote:i tried with gets instead of cin...it can handle null strings...i also submit my program as C instead of C++...but now i get Runtime Error (SIGSEGV) after 0.316 seconds of execution.... wat's the error supposed to mean? thanx
Code: Select all
for (i=0;i<blen;i++){
last=pos[i];
for (j=0;j<alen;j++){
if (a[j]==b[i]){
max=0;
for (k=i-1;k>=0;k--){
for (l=pos[k];l<pos[k+1];l++){
if (loc[l]<j && max<best[l]){
max=best[l];
}
}
}
best[last]=max+1;
loc[last]=j;
last++;
}
}
pos[i+1]=last;
}
Code: Select all
aaaa...(repeated 1000 times)
aaaa...(also repeated 1000 times)
Code: Select all
#include <stdio.h>
#include <string.h>
#define MAX 1001
int lcs( char *s1, char *s2 )
{
int i, j;
int dp[MAX][MAX] = { 0 };
int len1 = strlen( s1 ), len2 = strlen( s2 );
for( i = 1; i <= len1; ++i )
for( j = 1; j <= len2; ++j )
if( s1[i-1] == s2[j-1] ) dp[i][j] = dp[i-1][j-1] + 1;
else if( dp[i-1][j] >= dp[i][j-1] ) dp[i][j] = dp[i-1][j];
else dp[i][j] = dp[i][j-1];
return dp[len1][len2];
}
int main()
{
char s1[MAX], s2[MAX];
while( ( scanf( "%s", s1 ) !=EOF ) && ( scanf( "%s", s2 ) != EOF ) )
printf( "%d\n", lcs( s1, s2 ) );
return 0;
}
Code: Select all
while ( gets( s1 ) && gets( s2 ) )