-
-
Save Shafaet/11177430 to your computer and use it in GitHub Desktop.
LCS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAXC 1000 | |
char A[MAXC],B[MAXC]; | |
int lenA,lenB; | |
int dp[MAXC][MAXC]; | |
bool visited[MAXC][MAXC]; | |
int calcLCS(int i,int j) | |
{ | |
if(A[i]=='\0' or B[j]=='\0') return 0; | |
if(visited[i][j])return dp[i][j]; | |
int ans=0; | |
if(A[i]==B[j]) ans=1+calcLCS(i+1,j+1); | |
else | |
{ | |
int val1=calcLCS(i+1,j); | |
int val2=calcLCS(i,j+1); | |
ans=max(val1,val2); | |
} | |
visited[i][j]=1; | |
dp[i][j]=ans; | |
return dp[i][j]; | |
} | |
int main() { | |
scanf("%s%s",A,B); | |
lenA=strlen(A); | |
lenB=strlen(B); | |
printf("%d\n",calcLCS(0,0)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment