fork 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. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String s = "wwhoamiUrektxineabcdefghijklmno";
  13. String t = " ";
  14.  
  15. int head = 0, tail = 0;
  16. String worm = "";
  17. String caterpillar = "";
  18. do {
  19. while (head < s.length() && worm.indexOf(s.charAt(head)) < 0) {
  20. head++;
  21. worm = s.substring(tail, head);
  22. System.out.println(t.substring(0, tail) + worm);
  23. if (worm.length() > caterpillar.length())
  24. caterpillar = worm;
  25. }
  26. if (head < s.length())
  27. while (worm.indexOf(s.charAt(head)) >= 0) {
  28. tail++;
  29. worm = s.substring(tail, head);
  30. System.out.println(t.substring(0, tail) + worm + "(" + s.charAt(head) + ")");
  31. }
  32. } while (head < s.length());
  33. System.out.println("Longest: " + caterpillar);
  34. }
  35. }
Success #stdin #stdout 0.14s 38060KB
stdin
Standard input is empty
stdout
w
 (w)
 w
 wh
 who
 whoa
 whoam
 whoami
 whoamiU
 whoamiUr
 whoamiUre
 whoamiUrek
 whoamiUrekt
 whoamiUrektx
  hoamiUrektx(i)
   oamiUrektx(i)
    amiUrektx(i)
     miUrektx(i)
      iUrektx(i)
       Urektx(i)
       Urektxi
       Urektxin
        rektxin(e)
         ektxin(e)
          ktxin(e)
          ktxine
          ktxinea
          ktxineab
          ktxineabc
          ktxineabcd
           txineabcd(e)
            xineabcd(e)
             ineabcd(e)
              neabcd(e)
               eabcd(e)
                abcd(e)
                abcde
                abcdef
                abcdefg
                abcdefgh
                abcdefghi
                abcdefghij
                abcdefghijk
                abcdefghijkl
                abcdefghijklm
                abcdefghijklmn
                abcdefghijklmno
Longest: abcdefghijklmno