Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. /* Dynamic Programming C/C++ implementation of LCS problem */
  2. //#include<bits/stdc++.h>
  3.  
  4. //int max(int a, int b);
  5.  
  6. /* Returns length of LCS for X[0..m-1], Y[0..n-1] */
  7. //int lcs( char *X, char *Y, int m, int n )
  8. //{
  9. //    int L[m+1][n+1];
  10. //    int i, j;
  11.  
  12.     /* Following steps build L[m+1][n+1] in bottom up fashion. Note
  13.      that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
  14. /*    for (i=0; i<=m; i++)
  15.     {
  16.         for (j=0; j<=n; j++)
  17.         {
  18.             if (i == 0 || j == 0)
  19.                 L[i][j] = 0;
  20.            
  21.             else if (X[i-1] == Y[j-1])
  22.                 L[i][j] = L[i-1][j-1] + 1;
  23.            
  24.             else
  25.                 L[i][j] = max(L[i-1][j], L[i][j-1]);
  26.         }
  27.     }
  28.     */
  29.     /* L[m][n] contains length of LCS for X[0..n-1] and Y[0..m-1] */
  30. //    return L[m][n];
  31. //}
  32.  
  33. /* Utility function to get max of 2 integers */
  34. //int max(int a, int b)
  35. //{
  36. //    return (a > b)? a : b;
  37. //}
  38. //
  39. // Driver program to test above function
  40. //int main()
  41. /*{
  42.     char X[] = "AGGTAB";
  43.     char Y[] = "GXTXAYB";
  44.    
  45.     int m = strlen(X);
  46.     int n = strlen(Y);
  47.    
  48.     printf("Length of LCS is %d", lcs( X, Y, m, n ) );
  49.    
  50.     return 0;
  51. } */
  52.  
  53.  
  54.  
  55. //lcs(string a, string b, int n, int m)
  56.  
  57. /*function LCSLength(X[1..m], Y[1..n])
  58.  C = array(0..m, 0..n)
  59.  for i := 0..m
  60.  C[i,0] = 0
  61.  for j := 0..n
  62.  C[0,j] = 0
  63.  for i := 1..m
  64.  for j := 1..n
  65.  if X[i] = Y[j]
  66.  C[i,j] := C[i-1,j-1] + 1
  67.  else
  68.  C[i,j] := max(C[i,j-1], C[i-1,j])
  69.  return C[m,n]*/
  70.  
  71. #include<iostream>
  72. #include<string>
  73. #include<algorithm>
  74.  
  75. using namespace std;
  76. string a, b, s;
  77. int c[2002][2002];
  78.  
  79. //https://pastebin.com/ssKN9aKj
  80.  
  81. int main (){
  82.    
  83.     int t, n, m;
  84.     cin>> t;
  85.    
  86.     for(int k=0; k<t; k++){
  87.        
  88.         cin >> n>> m;
  89.         cin >> a;
  90.         cin >> b;   // Y: b   N: b[i]
  91.        
  92.         s = "";  // 清空s
  93. //        for(int i=0; i<n; i++)
  94. //        for(int i=0; i<m; i++)
  95.        
  96.         //    char ans[200];
  97.        
  98. //        cout << a <<b;
  99.        
  100. //        int cnt=0;
  101.         for(int i=1; i<=n; i++){
  102.             for(int j=1; j<=m;j++){
  103.                
  104.                  if(a[i-1]==b[j-1]){
  105.                     c[i][j]=c[i-1][j-1]+1;
  106.                     }
  107.                 else c[i][j]=max(c[i][j-1], c[i-1][j]); //DEBUGGGED  c[i-1][j-1]
  108.                 }
  109.         }
  110.         int nowi=n, nowj=m;
  111.            
  112.         while(nowi!=0&&nowj!=0){
  113.             if(a[nowi-1]==b[nowj-1]){  //DEBUGGED  b[nowj-1]
  114.                 s+=a[nowi-1];
  115.                 nowi--;
  116.                 nowj--;
  117.             }
  118.             else if(c[nowi-1]==0) nowj--;
  119.             else if(c[nowj-1]==0) nowi--;
  120.             else if(c[nowi-1][nowj]>c[nowi][nowj-1]) nowi--;
  121.             else nowj--;
  122.         }
  123.         reverse(s.begin(), s.end());
  124.         cout << s;
  125.    
  126.         if(c[n][m]==0) cout << "*";
  127.     //    else for(int i=0; i<cnt; i++)  cout << ans << endl;
  128.         cout << endl;
  129.        
  130.     }
  131. }
  132.  
  133. //char a = '4';
  134. //s[0]+=a;
  135. //reverse(a.begin(), a.end());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement