fork download
  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4. class Solution {
  5. public static void main(String[] args) throws Exception {
  6. char c;
  7. int n;
  8. boolean flag = true;
  9.  
  10. String s = "";
  11. while (true) {
  12. c = (char) br.read();
  13. if (c == '\n'||c==-1)
  14. break;
  15. if (Character.isAlphabetic(c) || c == ' ')
  16. s += c;
  17. }
  18. String[] str = s.split(" ");
  19. String[] t = s.toLowerCase().split(" ");
  20. n = Integer.parseInt(br.readLine());
  21. ArrayList<String> l = new ArrayList<String>();
  22. int u;
  23. for (int j = 0; j < n; j++)
  24. {
  25. String s1 = "";
  26. while (true) {
  27. u= br.read();
  28. c = (char)u;
  29. if (c == '\n'||u==-1)
  30. break;
  31. if (Character.isAlphabetic(c))
  32. s1 += c;
  33. }
  34. // System.out.println(s1);
  35. if(!l.contains(s1.toLowerCase()))
  36. l.add(s1.toLowerCase());
  37. }
  38. int i = 0;
  39. int siz = t.length;
  40. int b = 0, e = siz - 1, x = siz, y = 0;
  41. ArrayList<String> dup = new ArrayList<String>();
  42. while (i < siz) {
  43.  
  44. if (l.contains(t[i])) {
  45.  
  46. if (x > i)
  47. x = i;
  48. if (!dup.contains(t[i])) {
  49. dup.add(t[i]);
  50.  
  51. if (dup.size() == l.size()) {
  52.  
  53. flag = false;
  54. if ((i - x) < (e - b)) {
  55.  
  56. b = x;
  57. e = i;
  58. }
  59. dup.removeAll(dup);
  60.  
  61. i = x;
  62. x = i + 2;
  63. }
  64. } else {
  65. // System.out.println("$$"+i);
  66. x = i;
  67. dup.removeAll(dup);
  68.  
  69. dup.add(t[i]);
  70. }
  71.  
  72. }
  73.  
  74. i++;
  75.  
  76. }
  77.  
  78. if (flag)
  79. System.out.println("NO SUBSEGMENT FOUND");
  80. else {
  81. for (int w = b; w <= e; w++) {
  82. System.out.print(str[w] + " ");
  83. }
  84. }
  85.  
  86. }
  87. }
Success #stdin #stdout 0.22s 57804KB
stdin
x x a x a x b x b x c x a x x x x b xx b c xb c x cbxc x a x  b a x c z a  X C B CXZ A  Q ZA X X A  XC  x c as  c v b c as a c v  b vc  c c  sa a  as  c c vb b fg f d  sdxc x c v b  cx a   x c v b cx  a x c vc v bv b v c c x a  a s d c x cvv bb v  c x a qa  z x c v b c x z a q a z x c v f cd x s x x  c v c  cdf d x x c v  c xc x s a  a s d fd  fgg  v c c x x z as 
3
a
c
b

stdout
C B CXZ A