Advertisement
Dennnhhhickk

D

Nov 14th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. // ConsoleApplication3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. //#include "stdafx.h"
  5. #include <iostream>
  6. #include <math.h>
  7. #include <iomanip>
  8. #include <algorithm>
  9. #include <fstream>
  10. #include <string>
  11. #include <map>
  12.  
  13. using namespace std;
  14.  
  15. typedef long long ll;
  16. typedef unsigned long long ull;
  17. typedef long double ld;
  18. typedef string str;
  19.  
  20. const ll S = 10010;
  21. const ll T = 1010;
  22. const ll INF = 1e12;
  23.  
  24. str s, t;
  25. ll dp[S][T], nx[S];
  26. map <char, int> mp;
  27.  
  28. ifstream in;
  29. ofstream out;
  30.  
  31. int main()
  32. {
  33.     //in.open("input.txt");
  34.     //out.open("output.txt");
  35.     cin >> s >> t;
  36.     if (s[0] == t[0])
  37.         dp[0][0] = 1;
  38.     for (int i = 1; i < s.size(); i++)
  39.         for (int j = 0; j < t.size(); j++)
  40.                 if (j)
  41.                         dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j] + (s[i] == t[j] ? 1 : 0));
  42.                 else
  43.                     dp[i][j] = dp[i - 1][j] + (s[i] == t[j] ? 1 : 0);
  44.     /*for (int i = 0; i < s.size(); i++, out << endl)
  45.         for (int j = 0; j < t.size(); j++)
  46.             out << dp[i][j] << ' ';
  47.     /*ll mn = 0;
  48.     for (int i = 0; i < t.size(); i++)
  49.         if (dp[s.size() - 1][i] < dp[s.size() - 1][mn])
  50.             mn = i;*/
  51.     out << dp[s.size() - 1][t.size() - 1] << endl;
  52.     str ans = "";
  53.     ll i = s.size() - 1, j = t.size() - 1;
  54.     while (i != 0)
  55.     {
  56.         if (dp[i][j] == dp[i - 1][j] + 1 && dp[s.size() - 1][t.size() - 1])
  57.         {
  58.             i--;
  59.             dp[s.size() - 1][t.size() - 1]--;
  60.    
  61.             continue;
  62.         }
  63.         if (dp[i][j] == dp[i - 1][j - 1])
  64.             j--;
  65.         ans += s[i];
  66.         i--;
  67.     }
  68.     if (!dp[s.size() - 1][t.size() - 1])
  69.         ans += s[0];
  70.     reverse(ans.begin(), ans.end());
  71.     //out << s << endl;
  72.     cout << ans << endl;
  73.     //out << ans.size() << endl;
  74.     //system("pause");
  75.     return 0;
  76. }
  77.  
  78. /*
  79. abacaba
  80. aba
  81. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement