Advertisement
Guest User

Untitled

a guest
May 28th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define FOR(i,a,b) for(auto i=a; i!=b+1-2*(a>b); i+=1-2*(a>b))
  4. #define REP(i,a,b) for(auto i=a-(a>b); i!=b-(a>b); i+=1-2*(a>b))
  5. #define ALL(v) v.begin(),v.end()
  6. #define what_is(x) cout<<#x<<" is "<<x<<endl;
  7. #define min3(a,b,c) min(min(a,b),c)
  8. #define max3(a,b,c) max(max(a,b),c)
  9. #define SIZE 257
  10. #define MAXN 1000000007
  11. #define PI 3.141592653589793
  12. #define open_read1 freopen("C:\\Users\\Hepic\\Desktop\\a.txt","r",stdin)
  13. #define open_write1 freopen("C:\\Users\\Hepic\\Desktop\\b.txt","w",stdout)
  14. #define open_read freopen("rblock.in","r",stdin)
  15. #define open_write freopen("rblock.out","w",stdout)
  16.  
  17. using namespace std;
  18.  
  19.  
  20. typedef long long LL;
  21. typedef pair<int,int> PII;
  22.  
  23.  
  24.  
  25. int main()
  26. {
  27.     //open_read1;
  28.     string a,b,virus;
  29.     cin>>a>>b>>virus;
  30.  
  31.     int n=a.length(),m=b.length();
  32.     int dp[n+1][m+1],sub[n+1][m+1];
  33.  
  34.     for(int i=0; i<=n; i++)
  35.         fill(dp[i],dp[i]+m+1,0);
  36.     for(int i=0; i<=n; i++)
  37.         fill(sub[i],sub[i]+m+1,0);
  38.  
  39.     for(int i=1; i<=n; i++)
  40.     {
  41.         for(int j=1; j<=m; j++)
  42.         {
  43.             //cout<<a[i-1]<<" "<<b[j-1]<<endl;
  44.             if(a[i-1]==b[j-1])
  45.                 dp[i][j]=dp[i-1][j-1]+1,sub[i][j]=2;
  46.             else
  47.                 dp[i][j]=max(dp[i-1][j],dp[i][j-1]),sub[i][j]=1;
  48.             //cout<<dp[i][j]<<" ";
  49.         }
  50.         //cout<<endl;
  51.     }
  52.     string str;
  53.     int i=n,j=m,o=0;
  54.     while(1)
  55.     {
  56.         if(sub[i][j]==2)
  57.         {
  58.             //cout<<a[i-1]<<endl;
  59.             str.push_back(a[i-1]);
  60.             i--;
  61.             j--;
  62.             sub[i][j]=1;
  63.         }
  64.         else if(sub[i][j]==1)
  65.         {
  66.             if(dp[i-1][j]==dp[i][j])
  67.                 i--;
  68.             else if(dp[i][j-1]==dp[i][j])
  69.                 j--;
  70.         }
  71.         else if(sub[i][j]==0)
  72.             break;
  73.     }
  74.     //cout<<dp[n][m]<<endl;
  75.     string ans;
  76.     for(int i=str.length()-1; i>=0; i--)
  77.         ans.push_back(str[i]);
  78.  
  79.     while(1)
  80.     {
  81.         int temp,pos;
  82.         for(int i=0;i<ans.length();i++)
  83.         {
  84.             temp=0;
  85.             for(int j=0;j<virus.length();j++)
  86.             {
  87.                 if((i+j)<ans.length())
  88.                 {
  89.                     if(virus[j]==ans[i+j])
  90.                         temp++;
  91.                     if(temp==1)
  92.                         pos=i+j;
  93.                 }
  94.             }
  95.             if(temp==virus.length() && pos<ans.size())
  96.                 ans.erase(ans.begin()+pos);
  97.         }
  98.  
  99.  
  100.         //cout<<ans.length()<<endl;
  101.         if(ans.length()==0)
  102.         {
  103.             cout<<0<<endl;
  104.             return 0;
  105.         }
  106.  
  107.         else
  108.         {
  109.             cout<<ans<<endl;
  110.             return 0;
  111.         }
  112.     }
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement