Guest User

Untitled

a guest
Jun 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MOD 1000000007
  3. #define pb push_back
  4. #define bw(i,r,l) for (int i=r-1;i>=l;i--)
  5. #define fw(i,l,r) for (int i=l;i<r;i++)
  6. using namespace std;
  7. typedef pair<int,int> ii;
  8. string s,t; int dp[101][101];
  9. ii r[101][101];
  10. void get(int i,int j) {
  11. if (!i&&!j) return;
  12. ii temp=r[i][j];
  13. get(temp.first,temp.second);
  14. if (temp.first<i&&temp.second<j) cout<<s[temp.first];
  15. }
  16. signed main() {
  17. //freopen("aome.inp","r",stdin);
  18. ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  19. cin>>s>>t;
  20. memset(dp,-1,sizeof(dp));
  21. dp[0][0]=0;
  22. fw (i,0,s.length()+1) fw (j,0,t.length()+1) if (dp[i][j]!=-1) {
  23. if (i<s.length()) {
  24. if (dp[i][j]>dp[i+1][j]) {
  25. dp[i+1][j]=dp[i][j];
  26. r[i+1][j]=ii(i,j);
  27. }
  28. }
  29. if (j<t.length()) {
  30. if (dp[i][j]>dp[i][j+1]) {
  31. dp[i][j+1]=dp[i][j];
  32. r[i][j+1]=ii(i,j);
  33. }
  34. }
  35. if (i<s.length()&&j<t.length()) {
  36. if (s[i]==t[j]) {
  37. if (dp[i][j]+1>dp[i+1][j+1]) {
  38. dp[i+1][j+1]=dp[i][j]+1;
  39. r[i+1][j+1]=ii(i,j);
  40. }
  41. }
  42. }
  43. }
  44. cout<<dp[s.length()][t.length()]<<"\n";
  45. get(s.length(),t.length());
  46. return 0;
  47. }
Add Comment
Please, Sign In to add comment