fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define endl "\n"
  4. #define F first
  5. #define S second
  6. #define loop(a,n) for(int i=a; i<=n ; i++)
  7. #define TIME (1.0 * clock() / CLOCKS_PER_SEC)
  8. #define NAME "NUMBER"
  9. using namespace std;
  10. string x,y,t1,t2,t3;
  11. string dp[205][205];
  12. int m,n;
  13. string maxs(string a, string b) {
  14. if (a.size() > b.size()) return a;
  15. if (a.size() < b.size()) return b;
  16. return max(a,b);
  17. }
  18. string xoa0(string s) {
  19. int i = 0;
  20. while (i < s.size() - 1 && s[i] == '0') i++;
  21. return s.substr(i);
  22. }
  23.  
  24. void nhap() {
  25. cin >> x >> y;
  26. m = x.size();
  27. n = y.size();
  28. }
  29.  
  30. void solve() {
  31. for (int i = m; i >= 0; --i) {
  32. for (int j = n; j >= 0; --j) {
  33. if (i == m || j == n) dp[i][j] = "";
  34. else if (x[i] == y[j]) {
  35. t1 = x[i] + dp[i + 1][j + 1];
  36. t2 = dp[i + 1][j];
  37. t3 = dp[i][j + 1];
  38. dp[i][j] = maxs(t1, maxs(t2, t3));
  39. } else {
  40. dp[i][j] = maxs(dp[i + 1][j], dp[i][j + 1]);
  41. }
  42. }
  43. }
  44.  
  45. string res = xoa0(dp[0][0]);
  46. if (res.empty()) cout << -1 << endl;
  47. else cout << res << endl;
  48. }
  49.  
  50. int main(){
  51. ios_base::sync_with_stdio(0);
  52. cin.tie(0); cout.tie(0);
  53. freopen(NAME".INP","r",stdin);
  54. freopen(NAME".OUT","w",stdout);
  55. nhap();
  56. solve();
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
Standard output is empty