fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int max(int a,int b){
  4. int max;
  5. if(a>=b){max=a;}
  6. else{max=b;}
  7. return max;
  8. }
  9. int x[1002][1002];
  10. int main() {
  11. string a,b;
  12. getline(cin,a);
  13. getline(cin,b);
  14. for(int i = a.length();i>=0;i--){
  15. for(int j = b.length();j>=0;j--){
  16. if(i==a.length() or j==b.length()){
  17. x[i][j]=0;
  18. }
  19. else if(a[i]==b[j]){
  20. x[i][j]=x[i+1][j+1]+1;
  21. }
  22. else{
  23. x[i][j]=max(x[i][j+1],x[i+1][j]);
  24. }
  25. }
  26. }
  27. cout<<x[0][0]<<endl;
  28. for(int i = 0;i<a.length();i++){
  29. for(int j = 0;j<b.length();j++){
  30. cout<<x[i][j]<<' ';
  31. }
  32. cout<<endl;
  33. }
  34. cout<<a<<endl<<b<<endl;
  35. vector<char> v;
  36. int q=a.length()-1,w=b.length()-1,ans=0;
  37. while(ans!=x[0][0]){
  38. cout<<a[q]<<' '<<b[w]<<endl;
  39. if(x[q][w]==x[q][w-1]+1 && x[q][w]==x[q-1][w]+1 && x[q][w]==x[q-1][w-1]+1){ans++;v.push_back(a[q]);q--;w--;}
  40. else if(x[q][w-1]==x[q-1][w]){w--;q--;cout<<x[q][w]<<x[q-1][w-1]<<x[q-1][w]<<x[q][w-1]<<endl;}
  41. else if(x[q][w-1]>x[q-1][w]){w--;}
  42. else{q--;}
  43. }
  44. for(int i = ans-1;i>=0;i++){
  45. cout<<v[i];
  46. }
  47. cout<<endl;
  48. return 0;
  49. }
Runtime error #stdin #stdout 0s 4532KB
stdin
abcdefghijklm
ijklmdefghabc
stdout
5
5 5 5 5 5 5 4 3 3 3 3 2 1 
5 5 5 5 5 5 4 3 2 2 2 2 1 
5 5 5 5 5 5 4 3 2 1 1 1 1 
5 5 5 5 5 5 4 3 2 1 0 0 0 
5 4 4 4 4 4 4 3 2 1 0 0 0 
5 4 3 3 3 3 3 3 2 1 0 0 0 
5 4 3 2 2 2 2 2 2 1 0 0 0 
5 4 3 2 1 1 1 1 1 1 0 0 0 
5 4 3 2 1 0 0 0 0 0 0 0 0 
4 4 3 2 1 0 0 0 0 0 0 0 0 
3 3 3 2 1 0 0 0 0 0 0 0 0 
2 2 2 2 1 0 0 0 0 0 0 0 0 
1 1 1 1 1 0 0 0 0 0 0 0 0 
abcdefghijklm
ijklmdefghabc
m c
0000
l b
0000
k a
0000
j h
0110
i g
h g
g g
3433
f f
4544
e e
5555
d d
5555
c m
5555
b l
5-1106304908-19914906995
a k
a j
a i
 i