fork(1) download
  1. #include<iostream>
  2. #include<string.h>
  3. #define m 30
  4. using namespace std;
  5. char st1[]= "krgeeghtyksder";
  6. char st2[]="geekscoder";
  7. int fun(int** arr,char st1[],char st2[],int i,int j)
  8. {
  9. if((i>=strlen(st1))||(j>=strlen(st2)))
  10. return 0;
  11. if(arr[i][j]!=-1)
  12. return arr[i][j];
  13. if(st1[i]==st2[j])
  14. {
  15. arr[i][j]=1+fun(arr,st1,st2,i+1,j+1);
  16. return arr[i][j];
  17. }
  18. else
  19. {
  20. arr[i][j]=max(fun(arr,st1,st2,i+1,j),fun(arr,st1,st2,i,j+1));
  21. return arr[i][j];
  22. }
  23. }
  24. int main()
  25. {
  26. int **arr=new int*[m];
  27. for(int i=0;i<m;i++)
  28. {
  29. arr[i]=new int[m];
  30. }
  31. for(int i=0;i<m;i++)
  32. {
  33. for(int j=0;j<m;j++)
  34. {
  35. arr[i][j]=-1;
  36. }
  37. }
  38. cout<<fun(arr,st1,st2,0,0)<<" is the max commmon subseq.\n";
  39. return 0;
  40. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
8 is the max commmon subseq.