fork download
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6. public static void main(String[] args) throws FileNotFoundException {
  7. Scanner input = new Scanner(System.in);
  8. String[] parts;
  9.  
  10. while (input.hasNextLine()) {
  11. parts = input.nextLine().split(";");
  12.  
  13. System.out.println(
  14. findIntersection(parts[0].split(","), parts[1].split(","))
  15. );
  16. }
  17. }
  18.  
  19. private static String findIntersection(String[] arr1, String[] arr2) {
  20. StringBuilder result = new StringBuilder();
  21. int val1 = Integer.parseInt(arr1[0]);
  22. int val2 = Integer.parseInt(arr2[0]);
  23.  
  24. for (int i = 0, j = 0; ; ) {
  25. if (val1 == val2) {
  26. result.append(',').append(val1);
  27.  
  28. if (++i < arr1.length) {
  29. val1 = Integer.parseInt(arr1[i]);
  30. } else {
  31. break;
  32. }
  33.  
  34. if (++j < arr2.length) {
  35. val2 = Integer.parseInt(arr2[j]);
  36. } else {
  37. break;
  38. }
  39. } else if (val1 < val2) {
  40. if (++i < arr1.length) {
  41. val1 = Integer.parseInt(arr1[i]);
  42. } else {
  43. break;
  44. }
  45. } else {
  46. if (++j < arr2.length) {
  47. val2 = Integer.parseInt(arr2[j]);
  48. } else {
  49. break;
  50. }
  51. }
  52. }
  53.  
  54. return result.length() == 0 ? "" : result.substring(1);
  55. }
  56. }
Success #stdin #stdout 0.13s 321088KB
stdin
9,10,11;33,34,35
3,7,8,22;11,22
11,12,13,14;14,15,16
20,21,22;45,46,47
77,78,79;78,79,80,81,82
33,35;3,18,26,35
stdout
22
14

78,79
35