a53

SubsirComunMaximal

a53
Dec 27th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define NMax 1001
  5. #define maxim(a,b)((a>b)?a:b)
  6. using namespace std;
  7. int M,N;
  8. char A[NMax],B[NMax];
  9. int D[NMax][NMax];
  10.  
  11. int main(void)
  12. {
  13. int i,j;
  14. freopen("subsircomunmaximal.in","r",stdin);
  15. freopen("subsircomunmaximal.out","w",stdout);
  16. scanf("%s %s",A,B);
  17. M=strlen(A);
  18. N=strlen(B);
  19. for(i=1;i<=M;++i)
  20. for(j=1;j<=N;++j)
  21. if(A[i-1]==B[j-1])
  22. D[i][j]=1+D[i-1][j-1];
  23. else
  24. D[i][j]=maxim(D[i-1][j],D[i][j-1]);
  25. int L=0;
  26. char sir[1000];
  27. for(i=M,j=N;i;)
  28. if(A[i-1]==B[j-1])
  29. sir[L++]=A[i-1],--i,--j;
  30. else
  31. if(D[i-1][j]<D[i][j-1])
  32. --j;
  33. else
  34. --i;
  35. sir[L]='\0';
  36. reverse(sir,sir+L);
  37. printf("%s\n",sir);
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment