Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 for (int i=1; i<=n1; i++)
        for (int j=1; j<=n2; j++)
            if (s1[i] == s2[j])
                length[i][j] = length[i-1][j-1] + 1;
            else
                length[i][j] = max(length[i-1][j],
                                   length[i][j-1]);
 
  a b g d
e 0 0 0 0
f 0 0 0 0
g 0 0 1 1
d 0 0 1 2


What I have tried:

image of my thoughts
[Dynamic programming]
Posted
Updated 25-Sep-20 23:06pm
Comments
Richard MacCutchan 26-Sep-20 4:48am    
No idea, it's your code. Or perhaps you just copied some random code from the internet.
JacksonSteel 27-Sep-20 2:32am    
My code

To work that out, you will need to read and understand the algorithm.
This may help: Longest common subsequence problem - Wikipedia[^]

But ... once you think you do, start with the debugger, and make the change you suggest. Then test it with a variety of test data. Do you get the same results as the unchanged version? Are you identifying the LCS correctly? If you are for all cases, then great! You can write a paper on your simpler solution!
But ... if not then you know that you can't, and it's going to take you quite a bit of digging to work out why not ...

Enjoy the challenge! :thumbsup:
 
Share this answer
 
I suggest you get a copy of The C Programming Language - Wikipedia[^] and spend some time learning the language from the beginning.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900