fork(5) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static boolean checkOverlap(String a, String b, String c) {
  11. Boolean[][][] memoize = new Boolean[a.length()+1][b.length()+1][c.length()+1];
  12. return checkOverlap(a, b, c, 0, 0, 0, memoize);
  13. }
  14. private static boolean checkOverlap(
  15. , String b
  16. , String c
  17. , int pa
  18. , int pb
  19. , int pc
  20. , Boolean[][][] memoize
  21. ) {
  22. Boolean res = memoize[pa][pb][pc];
  23. if (res != null) {
  24. return (boolean)res;
  25. }
  26. if (pa == a.length() && pb == b.length() && pc == c.length()) {
  27. res = true;
  28. } else if (pc == c.length()) {
  29. res = false;
  30. } else {
  31. res = false;
  32. if (pa != a.length() && c.charAt(pc) == a.charAt(pa) && checkOverlap(a, b, c, pa+1, pb, pc+1, memoize)) {
  33. res = true;
  34. } else if (pb != b.length() && c.charAt(pc) == b.charAt(pb) && checkOverlap(a, b, c, pa, pb+1, pc+1, memoize)) {
  35. res = true;
  36. }
  37. }
  38. return (memoize[pa][pb][pc] = res);
  39. }
  40. public static void main (String[] args) throws java.lang.Exception
  41. {
  42. boolean r1 = checkOverlap("xxy", "xxz", "xxzxxy");
  43. System.out.println(r1);
  44. boolean r2 = checkOverlap("xxyxxy", "xxzxxz", "xxzxxyxxyxxz");
  45. System.out.println(r2);
  46. boolean r3 = checkOverlap("xxy", "xxy", "xxzxxyxxyxxz");
  47. System.out.println(r3);
  48. }
  49. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
true
true
false