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



Sample input:
4
aa
abba
abcd
abcde


Sample output:
1
6
12
18

Submission status: Accepted

Solution code:
def main_a():
    y, m, d = map(int, input().split())
    a = (y - 1) // 3
    b = (y - 1) % 3
    c = a * 590 + 195 * b 
    if y % 3:
        a = (m - 1) // 2
        b = (m - 1) % 2
        c += a * 39 + b * 20
    else:
        c += (m - 1) * 20
    c += d - 1
    print(196470 - c)
def a():
    n = int(input())
    for i in range(n):
        main_a()

def main_b():
    s = input()
    ans = []
    for i in range(1,len(s)):
        ans.append(s[:i] + s[i:])
        ans.append(s[i-1::-1] + s[i:])
        ans.append(s[i-1::-1] + s[len(s):i-1:-1])
        ans.append(s[:i] + s[len(s):i - 1:-1])
        ans.append(s[i:] + s[:i])
        ans.append(s[i:] + s[i - 1::-1])
        ans.append(s[len(s):i - 1:-1] + s[i - 1::-1])
        ans.append(s[len(s):i - 1:-1] + s[:i])
    print(len(set(ans)))

def b():
    n = int(input())
    for i in range(n):
        main_b()

b()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AOJ B

m = int(input())
for _ in range(m):
    s = list(input())
    all = []
    for i in range(1,len(s)):
        front = s[:i]
        back = s[i:]
        fr = list(reversed(front))
        br = list(reversed(back))
        pair = [front,fr,back,br]
        for j in range(4):
            if j > 1:
                for k in range(2):
                    p = pair[j] + pair[k]
                    if p not in all:
                        all.append(p)
            else:
                for k in range(2,4):
                    p = pair[j] + pair[k]
                    if p not in all:
                        all.append(p)

    if len(all) == 0:
        print(1)
    else:
        print(len(all))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
m = int(input())
for _ in range(m):
    s = input()
    ans = set()
    for i in range(1, len(s)):
        for j in range(4):
            ans.add(s[:i][::[-1, 1][j // 2]] + s[i:][::[-1, 1][j % 2]])
            ans.add(s[i:][::[-1, 1][j // 2]] + s[:i][::[-1, 1][j % 2]])
    print(len(ans))

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
n = int(sys.stdin.readline())
for i in range(n):
    s = list(sys.stdin.readline()[:-1])
    m = len(s)
    res = defaultdict(int)
    for i in range(1,m):
        k = s[:i]
        l = s[i:]
        k_ = k[::-1]
        l_ = l[::-1]
        res[tuple(k+l)] += 1
        res[tuple(k+l_)] += 1
        res[tuple(k_+l)] += 1
        res[tuple(k_+l_)] += 1
        res[tuple(l+k)] += 1
        res[tuple(l+k_)] += 1
        res[tuple(l_+k)] += 1
        res[tuple(l_+k_)] += 1
    print(len(list(res.keys())))

Submission status: Accepted

Solution code:
import itertools

def main():
    n = int(input())
    for _ in range(n):
        s = input()
        ssum = set()
        for i in range(len(s)):
            l = s[:i]
            rl = l[::-1]
            r = s[i:]
            rr = r[::-1]
            ssum.add(l+r)
            ssum.add(r+l)
            ssum.add(rl+r)
            ssum.add(r+rl)
            ssum.add(l+rr)
            ssum.add(rr+l)
            ssum.add(rl+rr)
            ssum.add(rr+rl)
        print(len(ssum))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def reconst(n):
    former=train[:n]
    latter=train[n:]
    formerRe="".join(reversed(former))
    latterRe="".join(reversed(latter))
    return [former+latterRe,formerRe+latter,formerRe+latterRe,latter+former,latterRe+former,latter+formerRe,latterRe+formerRe]

n = int(input())
for _ in range(n):
    s = set()
    train = input()
    s.add(train)
    leng = len(train)
    for i in range(1,leng):
        for t in reconst(i):
            if not t in s:
                s.add(t)
    print(len(s))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
# coding: utf-8
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))

Submission status: Runtime Error

Solution code:
=int(raw_input())

def comb(tr):
    l=len(tr)
    res=[]
    for i in range(l):
        head,tail=tr[i:],tr[:i]
        headrev=head[::-1]
        tailrev=tail[::-1]
        res.append(head+tail)
        res.append(headrev+tail)
        res.append(head+tailrev)
        res.append(headrev+tailrev)
        res.append(tail+head)
        res.append(tailrev+head)
        res.append(tail+headrev)
        res.append(tailrev+headrev)
    return len(set(res))
    #return res
for i in range(n):
    tr = raw_input()
    print comb(tr)