fork download
  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. final Scanner sc = new Scanner(System.in);
  5. while(sc.hasNext()) {
  6. final char[] s = sc.next().toCharArray();
  7. if(s[0] == '#')
  8. return;
  9. final Set<String> set = new TreeSet<String>();
  10. dfs(s, 0, set, new boolean[128]);
  11. System.out.println(set.size());
  12. int i = 0;
  13. for(final String ss : set) {
  14. if(i < 5 || set.size() - 5 <= i)
  15. System.out.println(ss);
  16. i++;
  17. }
  18. }
  19. }
  20. private static void dfs(final char[] cs, final int i, final Set<String> set, final boolean[] flags) {
  21. if(i == cs.length){
  22. set.add(new String(cs));
  23. return;
  24. }
  25. if(!flags[cs[i] + 1] && cs[i] < 'z') {
  26. flags[++cs[i]] = true;
  27. dfs(cs, i + 1, set, flags);
  28. flags[cs[i]--] = false;
  29. }
  30. if(cs[i] == 'a' || flags[cs[i]]) {
  31. dfs(cs, i + 1, set, flags);
  32. }
  33. }
  34. }
  35.  
Success #stdin #stdout 0.13s 380736KB
stdin
enw
abc
abcdefghijklmnopqrst
z
#
stdout
1
fox
5
acc
acd
bbd
bcc
bcd
17711
acceeggiikkmmooqqssu
acceeggiikkmmooqqstt
acceeggiikkmmooqqstu
acceeggiikkmmooqrrtt
acceeggiikkmmooqrrtu
bcdefghijklmnopqrrtt
bcdefghijklmnopqrrtu
bcdefghijklmnopqrssu
bcdefghijklmnopqrstt
bcdefghijklmnopqrstu
0