Problem:
Problem B:  Organize Your Train part II RJ Freight, a Japanese railroad company for freight operations
has recently constructed exchange lines at Hazawa, Yokohama.
The layout of the lines is shown in Figure B-1.   Figure B-1: Layout of the exchange lines  A freight train consists of 2 to 72 freight cars.  There are 26
types of freight cars, which are denoted by 26 lowercase letters
from "a" to "z".  The cars of the same type are indistinguishable from
each other, and each car's direction doesn't matter either.
Thus, a string of lowercase letters of length 2 to 72 is sufficient
to completely express the configuration of a train. Upon arrival at the exchange lines, a train is divided into two
sub-trains at an arbitrary position (prior to entering the
storage lines).  Each of the sub-trains may have its direction
reversed (using the reversal line).  Finally, the two sub-trains
are connected in either order to form the final configuration.
Note that the reversal operation is optional for each of the
sub-trains. For example, if the arrival configuration is "abcd", the train
is split into two sub-trains of either 3:1, 2:2 or 1:3 cars.
For each of the splitting, possible final configurations are
as follows ("+" indicates final concatenation position): [3:1]
    abc+d  cba+d  d+abc  d+cba
  [2:2]
    ab+cd  ab+dc  ba+cd  ba+dc  cd+ab  cd+ba  dc+ab  dc+ba
  [1:3]
    a+bcd  a+dcb  bcd+a  dcb+a  Excluding duplicates, 12 distinct configurations are possible. Given an arrival configuration, answer 
the number of distinct configurations which can be
constructed using the exchange lines described above. Input The entire input looks like the following.  the number of datasets = m  1st dataset   2nd dataset  ...  m-th dataset   Each dataset represents an arriving train, and is a string of
2 to 72 lowercase letters in an input line. Output For each dataset, output the number of possible train configurations
in a line.  No other characters should appear in the output. Sample Input 4
aa
abba
abcd
abcde Output for the Sample Input 1
6
12
18

Solution (Python):
m = int(input())
for _ in range(m):
    train = input()
    trains = set()
    for i in range(1, len(train)):
        mae = train[0:i]
        ato = train[i::]
        r_mae = mae[::-1]
        r_ato = ato[::-1]
        trains.add(mae + ato)
        trains.add(ato + mae)
        trains.add(mae + r_ato)
        trains.add(r_ato + mae)
        trains.add(r_mae + ato)
        trains.add(ato + r_mae)
        trains.add(r_ato + r_mae)
        trains.add(r_mae + r_ato)
    print(len(trains))

Solution (Python):
def main():
    num = int(input())
    for i in range(num):
        t = list(input())

        tlist = []
        for j in range(len(t) - 1):
            j += 1
            k = len(t) - j
            a = t[:j]
            b = t[-k:]
            arev = []
            brev = []
            for i in range(len(a)):
                j = len(a) - i - 1
                arev.append(a[j])
            for i in range(len(b)):
                j = len(b) - i - 1
                brev.append(b[j])
            app(tlist, a + b)
            app(tlist, a + brev)
            app(tlist, arev + b)
            app(tlist, arev + brev)
            app(tlist, b + a)
            app(tlist, b + arev)
            app(tlist, brev + a)
            app(tlist, brev + arev)
        print(len(tlist))
        
def app(tlist, new):
    for item in tlist:
        if item == new: return
    tlist.append(new)
    return

main()

Solution (Python):
m = int(input())
for _ in range(m):
    ss = set()
    s = input()
    length = len(s)
    for i in range(1, length):
        left = s[:i]
        right = s[i:]
        ss |= {left + right, left + right[::-1], left[::-1] + right, left[::-1] + right[::-1],
                right + left, right + left[::-1], right[::-1] + left, right[::-1] + left[::-1]}
    print(len(ss))

Solution (Python):
m, *S = open(0).read().split()
ans = []
for s in S:
    e = {s, s[::-1]}
    for x in range(1, len(s)):
        X = s[:x]; Y = s[x:]
        e.add(X[::-1]+Y)
        e.add(X+Y[::-1])
        e.add(X[::-1]+Y[::-1])
        e.add(Y+X)
        e.add(Y[::-1]+X)
        e.add(Y+X[::-1])
    ans.append("%d\n" % len(e))
open(1, 'w').writelines(ans)

Solution (Python):
m = int(input())
for _ in range(m):
    s = input()
    pat = set()
    for i in range(1,len(s)):
        f = s[:i]; r = s[i:]
        pat.add(f+r)
        pat.add(r+f)
        pat.add(f[::-1]+r[::-1])
        pat.add(r[::-1]+f[::-1])
        pat.add(f+r[::-1])
        pat.add(r+f[::-1])
        pat.add(f[::-1]+r)
        pat.add(r[::-1]+f)
    print(len(pat))

Solution (Python):
m = input()
for a in range(int(m)):
    b = set()
    s = input()
    for i in range(1,len(s)):
        t1 = s[:i]
        t2 = s[i:]
        b.add(t1+t2)
        b.add(t1+t2[::-1])
        b.add(t1[::-1]+t2)
        b.add(t1[::-1]+t2[::-1])
        b.add(t2+t1)
        b.add(t2+t1[::-1])
        b.add(t2[::-1]+t1)
        b.add(t2[::-1]+t1[::-1])
    print(len(b))

Solution (Python):

n=int(input())
for i in range(n):
    s=list(input())
    st=set()
    for j in range(1,len(s)):
        st.add((''.join(s[:j])+''.join(s[j:])))
        st.add((''.join(s[j:])+''.join(s[:j])))
        st.add((''.join(s[:j][::-1])+''.join(s[j:])))
        st.add((''.join(s[j:][::-1])+''.join(s[:j])))
        st.add((''.join(s[:j])+''.join(s[j:][::-1])))
        st.add((''.join(s[j:])+''.join(s[:j][::-1])))
        st.add((''.join(s[:j][::-1])+''.join(s[j:][::-1])))
        st.add((''.join(s[j:][::-1])+''.join(s[:j][::-1])))
    print(len(st))