fork(2) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string a;
  4. string b;
  5. int len;
  6. int f(int n,int m,int k)
  7. {
  8. if(n == 0 || m == 0) {
  9. return 0;
  10. }
  11. if(a[n-1] == b[m-1]) {
  12. if(k == len-1) {
  13. return f(n-1,m-1,k+1)+len; //if the common segment is of length len
  14. }
  15. else if(k >= len) {
  16. return f(n-1,m-1,k+1)+1; //if the common segment is greater than len
  17. }
  18. else { //if the segment has potential of becoming segment of length len
  19. int x = f(n-1,m-1,k+1);
  20. int y = max(f(n-1,m,0),f(n,m-1,0));
  21. return max(x,y);
  22. }
  23. }
  24. else {
  25. return max(f(n-1,m,0),f(n,m-1,0));
  26. }
  27. }
  28. int main()
  29. {
  30. int n;
  31. int m;
  32. int i;
  33. int j;
  34. int k;
  35.  
  36. while(1) {
  37. cin>>len; //length of minimum segment
  38. if(len == 0) {
  39. break;
  40. }
  41. cin>>a>>b;
  42. n = a.size();
  43. m = b.size();
  44. cout<<f(n,m,0)<<endl;
  45. }
  46.  
  47. }
  48.  
  49.  
Success #stdin #stdout 0s 2684KB
stdin
Standard input is empty
stdout
Standard output is empty