Problem:
Encryption System A programmer developed a new encryption system.
However, his system has an issue that
two or more distinct strings are `encrypted' to the same string. We have a string encrypted by his system. 
To decode the original string, we want to enumerate all the candidates of the string before the encryption.
Your mission is to write a program for this task. The encryption is performed taking the following steps. Given a string that consists only of lowercase letters ('a' to 'z').  Change the first 'b' to 'a'. If there is no 'b', do nothing.  Change the first 'c' to 'b'. If there is no 'c', do nothing. ... Change the first 'z' to 'y'. If there is no 'z', do nothing.  Input The input consists of at most 100 datasets.
Each dataset is a line containing an encrypted string.
The encrypted string consists only of lowercase letters, and contains at least 1 and at most 20 characters. The input ends with a line with a single '#' symbol. Output For each dataset,
the number of candidates n of the string before encryption should be printed in a line first,
followed by lines each containing a candidate of the string before encryption.
If n does not exceed 10, print all candidates in dictionary order;
otherwise, print the first five and the last five candidates in dictionary order. Here, dictionary order is recursively defined as follows.
The empty string comes the first in dictionary order.
For two nonempty strings x = x 1 ... x k and y = y 1 ... y l , 
the string x precedes the string y in dictionary order if  x 1 precedes y 1 in alphabetical order ('a' to 'z'), or  x 1 and y 1 are the same character and x 2 ... x k precedes y 2 ... y l in dictionary order.  Sample Input enw
abc
abcdefghijklmnopqrst
z
# Output for the Sample Input 1
fox
5
acc
acd
bbd
bcc
bcd
17711
acceeggiikkmmooqqssu
acceeggiikkmmooqqstt
acceeggiikkmmooqqstu
acceeggiikkmmooqrrtt
acceeggiikkmmooqrrtu
bcdefghijklmnopqrrtt
bcdefghijklmnopqrrtu
bcdefghijklmnopqrssu
bcdefghijklmnopqrstt
bcdefghijklmnopqrstu
0

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(s):
        d = collections.defaultdict(int)
        ac = {}
        ca = {}
        for i in range(26):
            t = int(2**i)
            c = chr(ord('a') + i)
            ac[t] = c
            ca[c] = t
        a = [ca[c] for c in s]
        z = ca['z']
        d[0] = 1
        for c in a:
            nd = collections.defaultdict(int)
            c2 = c * 2
            for k,v in d.items():
                if k & c or c == 1:
                    nd[k] += v
                if c != z and k & c2 == 0:
                    nd[k|c2] += v
            d = nd
        r = [sum(d.values())]

        ss = set([''])
        for t in s:
            ns = set()
            t2 = chr(ord(t) + 1)
            for si in ss:
                if t in si or t == 'a':
                    ns.add(si + t)
                if t != 'z' and t2 not in si:
                    ns.add(si + t2)
            ss = set(sorted(ns)[:5])
        rs = ss

        ss = set([''])
        for t in s:
            ns = set()
            t2 = chr(ord(t) + 1)
            for si in ss:
                if t in si or t == 'a':
                    ns.add(si + t)
                if t != 'z' and t2 not in si:
                    ns.add(si + t2)
            ss = set(sorted(ns, reverse=True)[:5])
        rs |= ss
        r.extend(sorted(rs))

        return r

    while True:
        s = S()
        if s == '
            break
        rr.extend(f(s))

    return '\n'.join(map(str, rr))



print(main())

Solution (Python):
ans = []
def search(s,c):
    if c == "a":
        ans.append(s)
        return

    for i in xrange(len(s)):
        if s[i] == c: return
    
        if s[i] == chr(ord(c)-1):
            t = list(s[:])
            t[i] = c
            t = "".join(t)
            
            search(t, chr(ord(c)-1))
    search(s, chr(ord(c)-1))
            
while 1:
    S = raw_input()
    if S == "

    ans = []
    search(S,"z")
    
    ans = sorted(list(set(ans)))
    print len(ans)
    if len(ans) <= 10:
        for a in ans:
            print a
    else:
        for i in xrange(5):
            print ans[i]
        for i in xrange(5):
            print ans[len(ans)-5+i]

Solution (Python):
ans=[""]
ans.pop()
s=""
w=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","}"]
x=[0]
x.pop()
def uni(seq):
    seen = set()
    seen_add = seen.add
    return [ x for x in seq if x not in seen and not seen_add(x)]

def check(a):
	global s
	global w
	n=len(a)
	
	for i in range(26):
		if i==0:
			continue
		a=a.replace(w[i],w[i-1],1);
	
	if a==s[0:n]:
		return True
	else:
		return False

def foo(a,n):
	
	global ans
	global x
	if n==len(a):
		if check(a):
			ans.append(a)
		return
	sz=len(a)
	t=a+s[sz]
	if check(t):
		foo(t,n)
	if w[x[sz]]=="z":
		return
	t=a+w[x[sz]+1]
	
	if check(t):
		
		foo(t,n)



while True:
	s = raw_input()
	if s=="
		break
	ans=[""]
	ans.pop()
	x=[0]
	x.pop()
	for i in range(len(s)):
		for j in range(26):
			if w[j]==s[i]:
				x.append(j)
	
	foo("",len(s))
	
	ans.sort()
	if len(ans)<=10:
		print len(ans)
		for i in range(len(ans)):
			print ans[i]
	else:
		print len(ans)
		for i in range(5):
			print ans[i]
		for i in range(5):
			print ans[len(ans)-5+i]

Solution (Python):
from itertools import chain

alph = "abcdefghijklmnopqrstuvwxyz"


def solve1(s):
    cands = [s]
    for c in reversed(alph[:-1]):
        cands = chain.from_iterable(
            [candidates(s, c) for s in cands]
        )

    cands = list(cands)
    cands.sort()
    print(len(cands))
    if len(cands) > 10:
        for s in cands[:5]:
            print(s)
        for s in cands[-5:]:
            print(s)
    else:
        for s in cands:
            print(s)
            
            
def next_char(c):
    return chr(ord(c)+1)

def candidates(string, key):
    nex = next_char(key)
    flag = False
    for i, c  in enumerate(string):
        if c == nex:
            flag = True
            break

        if c != key:
            continue

        yield string[:i] + nex + string[i+1:]

    if not flag:
        yield string
            



while True:
    s = input()
    if s == "
        break
    solve1(s)

Solution (Python):

import sys
def input():
    return sys.stdin.readline()[:-1]
while True:
    S = input()
    if S=="
        break
    N = len(S)
    Sn = [ord(c)-97 for c in S]
    ans = []
    ansappend = ans.append
    zip_ = list(zip(range(N-1, -1, -1), Sn))
    i = 0
    
    end = 1<<N
    Tst = [0] + [1]*25 + [0]
    while i < end:
        
        tst = Tst.copy()
        ans_ = []
        for j, n in zip_:
            if i>>j & 1:
                n += 1
                if tst[n]:
                    tst[n]=0
                    ans_.append(n)
                else:
                    i += 1<<j
                    break
            else:
                if tst[n]==0:
                    ans_.append(n)
                else:
                    i += 1<<j
                    break
        else:
            ansappend(ans_)
            i+=1
    
    print(len(ans))
    if len(ans)<=10:
        for ans_ in ans:
            print(*map(lambda x: chr(x+97), ans_), sep="")
    else:
        for ans_ in ans[:5]:
            print(*map(lambda x: chr(x+97), ans_), sep="")
        for ans_ in ans[-5:]:
            print(*map(lambda x: chr(x+97), ans_), sep="")