Saleh127

UVA 10100 / LCS

Feb 16th, 2022
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. /***
  2.  created: 2022-02-16-15.40.59
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11. string a,b;
  12. vector<string>x,y;
  13. ll dp[1005][1005];
  14.  
  15. ll solve(ll i,ll j)
  16. {
  17.      if(i>=x.size() || j>=y.size()) return 0;
  18.  
  19.      if(dp[i][j]!=-1) return dp[i][j];
  20.  
  21.      ll ans=0;
  22.  
  23.      if(x[i]==y[j])
  24.      {
  25.           ans=max(ans,1+solve(i+1,j+1));
  26.      }
  27.      else
  28.      {
  29.           ans=max(ans,solve(i,j+1));
  30.           ans=max(ans,solve(i+1,j));
  31.      }
  32.  
  33.      return dp[i][j]=ans;
  34.  
  35.  
  36. }
  37.  
  38. int main()
  39. {
  40.    ios_base::sync_with_stdio(0);
  41.    cin.tie(0);cout.tie(0);
  42.  
  43.  
  44.    ll cs=1;
  45.  
  46.    while(getline(cin,a))
  47.    {
  48.         if(a=="#") break;
  49.  
  50.         getline(cin,b);
  51.  
  52.         x.clear(),y.clear();
  53.         memset(dp,-1,sizeof dp);
  54.  
  55.         for(ll i=0;i<a.size();i++)
  56.         {
  57.              if((a[i]>='a' && a[i]<='z') || (a[i]>='A' && a[i]<='Z') || (a[i]>='0' && a[i]<='9'))
  58.              {
  59.                   continue;
  60.              }
  61.              else
  62.              {
  63.                   a[i]=' ';
  64.              }
  65.         }
  66.  
  67.         for(ll i=0;i<b.size();i++)
  68.         {
  69.              if((b[i]>='a' && b[i]<='z') || (b[i]>='A' && b[i]<='Z') || (b[i]>='0' && b[i]<='9'))
  70.              {
  71.                   continue;
  72.              }
  73.              else
  74.              {
  75.                   b[i]=' ';
  76.              }
  77.         }
  78.  
  79.         string c;
  80.  
  81.         istringstream ss(a);
  82.  
  83.         while(ss>>c)
  84.         {
  85.              x.push_back(c);
  86.         }
  87.  
  88.         istringstream sss(b);
  89.  
  90.         while(sss>>c)
  91.         {
  92.              y.push_back(c);
  93.         }
  94.  
  95.         cout.width(2);
  96.         cout<<cs++<<". ";
  97.  
  98.         if(x.size()==0 || y.size()==0) cout<<"Blank!"<<nl;
  99.  
  100.         else cout<<"Length of longest match: "<<solve(0,0)<<nl;
  101.    }
  102.  
  103.    get_lost_idiot;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment