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: Wrong Answer

Solution code:
m = int(input())
for i in range(4):
    d = input()
    trains = [d]
    for j in range(1, len(d)):
        trains.extend([d[:j][::-1] + d[j:], d[:j] + d[j:][::-1], d[:j][::-1] + d[j:][::-1], d[j:] + d[:j], d[j:][::-1] + d[:j], d[j:] + d[:j][::-1], d[j:][::-1] + d[:j][::-1]])
    print(len(set(trains)))

Submission status: Wrong Answer

Solution code:
ls = []

def add(t):
    if t not in ls:
        ls.append(t)

n = int(input())
for i in range(n):
    t = input()[:-1]
    ls = [t] 
    for j in range(1,len(t)):
        f, b = t[:j], t[j:]
        add(f+b[::-1])
        add(f[::-1]+b)
        add(f[::-1]+b[::-1])
        add(b+f)
        add(b+f[::-1])
        add(b[::-1]+f)
        add(b[::-1]+f[::-1])
    print(len(ls))

Submission status: Wrong Answer

Solution code:
#coding: utf-8

n = int(raw_input())
for i in range(n):
    s = raw_input()
    l = set([s])
    for k in range(len(s)-1):
        t1 = s[:k+1]
        t2 = s[k+1:]
        n1 = t1 + t2[::-1]
        n2 = t1[::-1] + t2
        n3 = t1[::-1] + t2[::-1]
        n4 = t2 + t1[::-1]
        n5 = t2[::-1] + t1
        n6 = t2[::-1] + t1[::-1]
        n7 = t2 + t1
        l2 = set([n1,n2,n3,n4,n5,n6,n7])
        l = l.union(l2)
    print len(l)
    print l

Submission status: Accepted

Solution code:
m=int(input())
ans=[]
for i in range(m):
  train=input()
  index=len(train)
  count=0
  tmp_a=set([])
  for i in range(1,index):
    tmp_a.add(train[:i]+train[i:])
    tmp_a.add(train[i:]+train[:i])
    tmp_a.add(train[:i][::-1]+train[i:])
    tmp_a.add(train[i:][::-1]+train[:i])
    tmp_a.add(train[:i]+train[i:][::-1])
    tmp_a.add(train[i:]+train[:i][::-1])
    tmp_a.add(train[:i][::-1]+train[i:][::-1])
    tmp_a.add(train[i:][::-1]+train[:i][::-1])
  ans.append(len(tmp_a))
for i in ans:
  print(i)

Submission status: Accepted

Solution code:
m = int(input())
for i in range(m):
    s = list(input())
    kouho = []
    for i in range(1, len(s)):
        this1 = s[:i]
        this2 = s[i:]
        kouho.append([this1 + this2])
        kouho.append([this1[::-1] + this2])
        kouho.append([this1[::-1] + this2[::-1]])
        kouho.append([this1 + this2[::-1]])
        kouho.append([this2 + this1])
        kouho.append([this2[::-1] + this1])
        kouho.append([this2[::-1] + this1[::-1]])
        kouho.append([this2 + this1[::-1]])
    kouho.sort()
    #print(len(kouho))
    cnt = 0
    i = 1
    while i < len(kouho):
        if kouho[i] == kouho[i - 1]:
            del kouho[i]
            i -= 1
        i += 1
    print(len(kouho))

Submission status: Accepted

Solution code:
def solve(s):
	res = set([s])
	for split_at in range(1, len(s)):
		a = s[:split_at]
		b = s[split_at:]
		res.add(a[::-1] + b)
		res.add(a + b[::-1])
		res.add(a[::-1] + b[::-1])
		res.add(b + a)
		res.add(b[::-1] + a)
		res.add(b + a[::-1])
		res.add(b[::-1] + a[::-1])
	return len(res)

N = int(input())
for _ in range(N):
	print(solve(input()))

Submission status: Accepted

Solution code:
for i in range(int(input())):
    d = input()
    s = {d, d[::-1]}
    for j in range(1, len(d)):
        da, db = d[:j], d[j:]
        dar, dbr = da[::-1], db[::-1]
        s.add(da + dbr)
        s.add(dar + db)
        s.add(dar + dbr)
        s.add(db + da)
        s.add(db + dar)
        s.add(dbr + da)
    print(len(s))

Submission status: Accepted

Solution code:
m = int(input())
for i in range(m):
    d = input()
    trains = [d]
    for j in range(1, len(d)):
        trains.extend([d[:j][::-1] + d[j:], d[:j] + d[j:][::-1], d[:j][::-1] + d[j:][::-1], d[j:] + d[:j], d[j:][::-1] + d[:j], d[j:] + d[:j][::-1], d[j:][::-1] + d[:j][::-1]])
    print(len(set(trains)))

Submission status: Accepted

Solution code:
m = int(input())
for i in range(m):
    d = input()
    trains = [d]
    for j in range(1, len(d)):
        f, b = d[:j], d[j:]
        rf, rb = f[::-1], b[::-1]
        trains.extend([rf+b, f+rb, rf+rb, b+f, rb+f, b+rf, rb+rf])
    print(len(set(trains)))

Submission status: Accepted

Solution code:





def add(l1,s1,s2):
	plus=s1+s2
	if plus in l1:
		return
	l1.append(plus)
	#print tui," ADD!!"
	#print tui


num=int(raw_input())


for i in range(num):
	l1=[]
	word=raw_input()


	n=1
	while n<len(word):

		s1=word[0:n]
		s2=word[n:]

		add(l1,s1,s2)
		add(l1,s1[::-1],s2)
		add(l1,s1,s2[::-1])
		add(l1,s1[::-1],s2[::-1])
		add(l1,s2,s1)
		add(l1,s2[::-1],s1)
		add(l1,s2,s1[::-1])
		add(l1,s2[::-1],s1[::-1])
		#print
		

		n+=1


	print len(l1)

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        m = input()
        for i in range(m):
            s = raw_input()
            count = set()
            for i in range(1, len(s)):
                l = s[:i]
                r = s[i:]
                count.add(l + r)
                count.add(l + r[::-1])
                count.add(l[::-1] + r)
                count.add(l[::-1] + r[::-1])
                count.add(r + l)
                count.add(r + l[::-1])
                count.add(r[::-1] + l)
                count.add(r[::-1] + l[::-1])
            print len(count)
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Accepted

Solution code:

def reverse(s):
    return ''.join(list(reversed(s)))

m = int(input())

for i in range(m):
    d = input()
    _set = set()
    for i in range(len(d)-1):
        w1 = d[:i+1]
        w2 = d[i+1:]
        _set.add(w1 + w2)
        _set.add(w2 + w1)
        _set.add(reverse(w1) + reverse(w2))
        _set.add(reverse(w2) + reverse(w1))
        _set.add(w1 + reverse(w2))
        _set.add(w2 + reverse(w1))
        _set.add(reverse(w1) + w2)
        _set.add(reverse(w2) + w1)

    print(len(_set))

Submission status: Accepted

Solution code:
m = int(input())

for i in range(m):
    d = input()
    _set = set()
    for i in range(len(d)-1):
        w1 = d[:i+1]
        w2 = d[i+1:]
        w1_r = w1[::-1]
        w2_r = w2[::-1]
        _set.add(w1 + w2)
        _set.add(w2 + w1)
        _set.add(w1_r+w2_r)
        _set.add(w2_r+w1_r)
        _set.add(w1 + w2_r)
        _set.add(w2 + w1_r)
        _set.add((w1_r) + w2)
        _set.add((w2_r) + w1)

    print(len(_set))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from itertools import product
m = int(input())

for i in range(m):
    train = list(input())
    train_set = set()
    for j in range(0,len(train)):
        A = ''.join(train[:j+1])
        B = ''.join(train[j+1:])
        invA = ''.join(reversed(train[:j+1]))
        invB = ''.join(reversed(train[j+1:]))
        for t in product((A,invA),(B,invB)):
            train_set.add(''.join(t))
        for t in product((B,invB),(A,invA)):
            train_set.add(''.join(t))
    print(len(train_set))

Submission status: Accepted

Solution code:
n = int(raw_input())
for i in range(n):
	s = raw_input()
	length = len(s)
	organize_set = set([s, s[::-1]])
	for j in range(1,length):
		f = s[:j]
		frev = f[::-1]
		b = s[j:]
		brev = b[::-1] 
		organize_set.add(f + brev)
		organize_set.add(b + f)
		organize_set.add(brev + f)
		organize_set.add(frev + b)
		organize_set.add(frev + brev)
		organize_set.add(b + frev)
	print len(organize_set)

Submission status: Accepted

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

n = int(raw_input())
for i in range(n):
    ft = raw_input()
    s = set([ft])
    for j in range(1, len(ft)):
        c1 = ft[j:]
        c2 = ft[:j]
        s.add(c1+c2)
        s.add(c1[::-1]+c2)
        s.add(c1[::-1]+c2[::-1])
        s.add(c1+c2[::-1])
        s.add(c2+c1)
        s.add(c2[::-1]+c1)
        s.add(c2[::-1]+c1[::-1])
        s.add(c2+c1[::-1])

    print len(s)

Submission status: Accepted

Solution code:
ls = []

def add(t):
    if t not in ls:
        ls.append(t)

n = int(input())
for i in range(n):
    t = input()
    ls = [t] 
    for j in range(1,len(t)):
        f, b = t[:j], t[j:]
        add(f+b[::-1])
        add(f[::-1]+b)
        add(f[::-1]+b[::-1])
        add(b+f)
        add(b+f[::-1])
        add(b[::-1]+f)
        add(b[::-1]+f[::-1])
    print(len(ls))

Submission status: Accepted

Solution code:
__author__ = 'Gen'

n = int(raw_input(""))
for i in xrange(0, n):
    text = raw_input("")
    found = [text]
    count = 1
    rev = [(1, 1), (-1, 1), (1, -1), (-1, -1)]
    for i in xrange(0, len(text)):
        for r in rev:
            if text[0:i][::r[0]] + text[i:len(text)][::r[1]] not in found:
                count += 1
                found.append(text[0:i][::r[0]] + text[i:len(text)][::r[1]])
            if text[i:len(text)][::r[0]] + text[0:i][::r[1]] not in found:
                count += 1
                found.append(text[i:len(text)][::r[0]] + text[0:i][::r[1]])
    print count

Submission status: Accepted

Solution code:
def prime_list(n):
    p = [True for i in range(n)]
    p[0] = False
    p[1] = False
    for j in range(4, n, 2):
            p[j] = False
    i = 3
    while i*i < n:
        if p[i]:
            for j in range(2*i, n, i):
                p[j] = False
        i += 2
    p_list = [2*i+1 for i in range(1,n//2) if p[2*i+1]]
    p_list.add(2)
    return p_list

def main():
    m = int(input())
    for _ in range(m):
        s = input()
        S = set()
        l = len(s)
        for i in range(1, l):
            S.add(s[0:i]+s[i:l])
            S.add(s[0:i][::-1]+s[i:l])
            S.add(s[0:i]+s[i:l][::-1])
            S.add(s[0:i][::-1]+s[i:l][::-1])
            S.add(s[i:l]+s[0:i])
            S.add(s[i:l][::-1]+s[0:i])
            S.add(s[i:l]+s[0:i][::-1])
            S.add(s[i:l][::-1]+s[0:i][::-1])
        print(len(S))
        # print(S)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
	s = input()

	ss = set()

	for i in range(len(s)-1):
		s1 = s[0:i+1]
		s2 = s[i+1:]
		ss.add(s1+s2)
		ss.add(s1+s2[::-1])
		ss.add(s1[::-1]+s2)
		ss.add(s1[::-1]+s2[::-1])
		ss.add(s2+s1)
		ss.add(s2+s1[::-1])
		ss.add(s2[::-1]+s1)
		ss.add(s2[::-1]+s1[::-1])
	print(len(ss))

Submission status: Accepted

Solution code:
n = input()

for _ in range(n):
    s = raw_input()

    bag = set()
    for i in range(len(s)):
        head = s[:i]
        tail = s[i:]

        bag.add(head + tail)
        bag.add(head + tail[:: -1])
        bag.add(head[:: -1] + tail)
        bag.add(head[:: -1] + tail[:: -1])
        bag.add(tail + head)
        bag.add(tail + head[:: -1])
        bag.add(tail[:: -1] + head)
        bag.add(tail[:: -1] + head[:: -1])

    print len(bag)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding: utf-8

n = int(raw_input())
for i in range(n):
    s = raw_input()
    l = set([s])
    for k in range(len(s)-1):
        t1 = s[:k+1]
        t2 = s[k+1:]
        n1 = t1 + t2[::-1]
        n2 = t1[::-1] + t2
        n3 = t1[::-1] + t2[::-1]
        n4 = t2 + t1[::-1]
        n5 = t2[::-1] + t1
        n6 = t2[::-1] + t1[::-1]
        n7 = t2 + t1
        l2 = set([n1,n2,n3,n4,n5,n6,n7])
        l = l.union(l2)
    print len(l)

Submission status: Accepted

Solution code:
for _ in range(int(input())):
    s = input()
    l = set([s,s[::-1]])
    for i in range(1,len(s)):
        a,b,ra,rb = s[:i],s[i:],s[:i][::-1],s[i:][::-1]
        l |= set([a+rb,ra+b,ra+rb,b+a,b+ra,rb+a])
    print(len(l))

Submission status: Accepted

Solution code:
M = int(raw_input())
for _ in xrange(M):
    S = raw_input()
    a = {S}
    for i in xrange(len(S)):
        L = S[:i]
        R = S[i:]
        L2 = L[::-1]
        R2 = R[::-1]
        a |= {L + R2, L2 + R, L2 + R2, R + L, R + L2, R2 + L, R2 + L2}
    print(len(a))

Submission status: Accepted

Solution code:
import collections


for t in range(int(input())):
    res = collections.defaultdict(int)
    s = input()
    for i in range(len(s)-1):
        s1 = s[:i+1]
        s2 = s[i+1:]

        res[s1+s2] += 1
        res[s1[::-1]+s2] += 1
        res[s1+s2[::-1]] += 1
        res[s1[::-1]+s2[::-1]] += 1

        res[s2+s1] += 1
        res[s2[::-1]+s1] += 1
        res[s2+s1[::-1]] += 1
        res[s2[::-1]+s1[::-1]] += 1

    print(len(res))

Submission status: Accepted

Solution code:
import sys

def solve():
    m = int(sys.stdin.readline().rstrip())

    for i in range(m):
        train = sys.stdin.readline().rstrip()
        n = len(train)
        s = set()
        s.add(train)
        s.add(train[::-1])

        for j in range(1, n):
            # s.add(train[:j] + train[j:]) = train
            s.add(train[:j][::-1] + train[j:])
            s.add(train[:j] + train[j:][::-1])
            s.add(train[:j][::-1] + train[j:][::-1])

            s.add(train[j:] + train[:j])
            s.add(train[j:][::-1] + train[:j])
            s.add(train[j:] + train[:j][::-1])
            # s.add(train[j:][::-1] + train[:j][::-1]) = train[::-1]

        ans = len(s)
        print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# coding: utf-8
for i in range(int(input())):
    data = input()
    d_set = set()
    for i in range(len(data)-1):
        str1 = data[ 0 : i+1 ]
        str2 = data[ i+1 : len(data) ]
        d_set.add(str1+str2)
        d_set.add(str1+str2[::-1])
        d_set.add(str1[::-1]+str2)
        d_set.add(str1[::-1]+str2[::-1])
        
        d_set.add(str2+str1)
        d_set.add(str2+str1[::-1])
        d_set.add(str2[::-1]+str1)
        d_set.add(str2[::-1]+str1[::-1])
    print(len(d_set))

Submission status: Accepted

Solution code:
def reverseStr(s):
    return s[::-1]

n = int(input())
for i in range(n):
    s = input()
    d = set([])
    for j in range(1,len(s)):
        a = s[:j]
        b = s[j:]
        d.add(a+b)
        d.add(b+a)
        d.add(a + reverseStr(b))
        d.add(b + reverseStr(a))
        d.add(reverseStr(a) + b)
        d.add(reverseStr(b) + a)
        d.add(reverseStr(a) + reverseStr(b))
        d.add(reverseStr(b) + reverseStr(a))
    print(len(d))

Submission status: Accepted

Solution code:
N = int(input())
src = [input() for i in range(N)]
for s in src:
    st = set([s])
    for n in range(1,len(s)):
        s1,s2 = s[:n], s[n:]
        r1,r2 = s1[::-1], s2[::-1]
        st.add(s1 + r2)
        st.add(r1 + s2)
        st.add(r1 + r2)
        st.add(s2 + s1)
        st.add(s2 + r1)
        st.add(r2 + s1)
        st.add(r2 + r1)
    print(len(st))

Submission status: Accepted

Solution code:
N = int(input())
for _ in range(N):
    train = input()
    result = set([])
    for i in range(1,len(train)):
        ft = train[:i]
        bt = train[i:]
        if ft + bt not in result: result.add(ft+bt)
        if ft[::-1] + bt not in result : result.add(ft[::-1]+bt)
        if ft + bt[::-1] not in result : result.add(ft+bt[::-1])
        if ft[::-1] + bt[::-1] not in result:result.add(ft[::-1] + bt[::-1])
        if bt + ft not in result : result.add(bt+ft)
        if bt[::-1] + ft not in result : result.add(bt[::-1]+ft)
        if bt + ft[::-1] not in result : result.add(bt+ft[::-1])
        if bt[::-1] + ft[::-1] not in result:result.add(bt[::-1] + ft[::-1])
    print(len(result))

Submission status: Accepted

Solution code:
m = int(input())
for k in range(m):
    s = input()
    strs = set([s, s[::-1]])
    for i in range(1, len(s)):
        f, b = s[0 : i], s[i :]
        strs.add(b + f)
        strs.add(f[::-1] + b[::-1])
        strs.add(b + f[::-1])
        strs.add(f + b[::-1])
        strs.add(b[::-1] + f)
        strs.add(f[::-1] + b)
    print(len(strs))

Submission status: Accepted

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

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 = []

    n = I()
    t = 0
    while t < n:
        t += 1
        s = S()
        e = set()
        for i in range(len(s)):
            s1 = s[:i]
            s2 = s[i:]
            sr1 = s1[::-1]
            sr2 = s2[::-1]
            e.add(s1+s2)
            e.add(sr1+s2)
            e.add(s1+sr2)
            e.add(sr1+sr2)
            e.add(s2+s1)
            e.add(sr2+s1)
            e.add(s2+sr1)
            e.add(sr2+sr1)


        rr.append(len(e))

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


print(main())

Submission status: Accepted

Solution code:
i = input()

int_i = int(i)

for k in range(int_i):
    arr_train = input()
    len_train = len(arr_train)
    last_train = []
    for j in range(1, len_train):
        bef_train = arr_train[:j:]
        af_train = arr_train[j::]
        last_train.append(bef_train+af_train)
        last_train.append(bef_train[::-1]+af_train)
        last_train.append(bef_train+af_train[::-1])
        last_train.append(bef_train[::-1]+af_train[::-1])
        last_train.append(af_train+bef_train)
        last_train.append(af_train[::-1]+bef_train)
        last_train.append(af_train+bef_train[::-1])
        last_train.append(af_train[::-1]+bef_train[::-1])
    uniq_train = list(set(last_train))
    print(len(uniq_train))

Submission status: Accepted

Solution code:
for _ in range(int(input())):
    s=input()
    l=set()
    for i in range(1,len(s)+1):
        e=[
            s[:i]+s[i:],
            s[:i]+s[i:][::-1],
            s[:i][::-1]+s[i:],
            s[:i][::-1]+s[i:][::-1],
            s[i:]+s[:i],
            s[i:]+s[:i][::-1],
            s[i:][::-1]+s[:i],
            s[i:][::-1]+s[:i][::-1]
        ]
        l=l|set(e)
    print(len(l))

Submission status: Accepted

Solution code:
def comp(sf, s1, s2):
    s = s1+s2
    if not(s in sf): sf.append(s)

m = int(raw_input())
for i in xrange(m):
    s = raw_input()
    c = ['', '']
    sf = []
    for j in xrange(1, len(s)):
        c = s[:j],s[j:]
        comp(sf, c[0], c[1])
        comp(sf, c[0], c[1][::-1])
        comp(sf, c[0][::-1], c[1])
        comp(sf, c[0][::-1], c[1][::-1])
        comp(sf, c[1], c[0])
        comp(sf, c[1], c[0][::-1])
        comp(sf, c[1][::-1], c[0])
        comp(sf, c[1][::-1], c[0][::-1])

    print len(sf)

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline())

for line in sys.stdin:
    line = line.strip()
    L = len(line)
    cand = set()
    for i in xrange(L):
        head = line[:i]
        tail = line[i:]
        cand.add(head + tail)
        cand.add(head[::-1] + tail)
        cand.add(head + tail[::-1])
        cand.add(head[::-1] + tail[::-1])
        cand.add(tail + head)
        cand.add(tail[::-1] + head)
        cand.add(tail + head[::-1])
        cand.add(tail[::-1] + head[::-1])
    print len(cand)

Submission status: Accepted

Solution code:
for i in range(input()):
    s=raw_input()
    S=set()
    for j in range(1,len(s)):
        S.add(s[:j]+s[j:])
        S.add(s[:j][::-1]+s[j:])
        S.add(s[:j]+s[j:][::-1])
        S.add(s[:j][::-1]+s[j:][::-1])
        S.add(s[j:]+s[:j])
        S.add(s[j:][::-1]+s[:j])
        S.add(s[j:]+s[:j][::-1])
        S.add(s[j:][::-1]+s[:j][::-1])
    print len(S)

Submission status: Accepted

Solution code:

m = input()
for cnt in range(m):
  mem = {}
  s = raw_input()
  for i in range(1, len(s)):
    s1 = s[:i]
    s2 = s[i:]
    mem[s1 + s2] = True
    mem[s1[::-1] + s2] = True
    mem[s1 + s2[::-1]] = True
    mem[s1[::-1] + s2[::-1]] = True
    mem[s2 + s1] = True
    mem[s2 + s1[::-1]] = True
    mem[s2[::-1] + s1] = True
    mem[s2[::-1] + s1[::-1]] = True
  print len(mem)

Submission status: Accepted

Solution code:
for i in range(input()):
	train = raw_input()
	ans = set()
	for i in range(len(train)):
		f = train[:i]; rf = f[::-1]
		b = train[i:]; rb = b[::-1]
		ans |= set([f+b,rf+b,f+rb,rf+rb,b+f,rb+f,b+rf,rb+rf])
	print len(ans)

Submission status: Accepted

Solution code:
#20:45



num = int(input())

for i in range(num):
    def count_pattern(prefix,suffix):

        answer.add(prefix + suffix)
        answer.add(prefix[::-1] + suffix)
        answer.add(prefix + suffix[::-1])
        answer.add(prefix[::-1] + suffix[::-1])

        answer.add(suffix + prefix)
        answer.add(suffix[::-1] + prefix)
        answer.add(suffix + prefix[::-1])
        answer.add(suffix[::-1] + prefix[::-1])
    
    target = input()
    answer = set()
    for j in range(1,len(target)):
        target_prefix = target[:j]
        target_suffix = target[j:]
        count_pattern(target_prefix,target_suffix)
    print(len(answer))

Submission status: Accepted

Solution code:
def rename(a, b) :
    new = []
    re_a = list(reversed(a))
    re_b = list(reversed(b))
    new.append(''.join(a + b))
    new.append(''.join(re_a + b))
    new.append(''.join(a + re_b))
    new.append(''.join(re_a + re_b))
    new.append(''.join(b + a))
    new.append(''.join(re_b + a))
    new.append(''.join(b + re_a))
    new.append(''.join(re_b + re_a))
    return list(set(new))

m = int(input())
for i in range(m) :
    train = list(input())
    new_train = []
    for j in range(1, len(train)) :
        pre_train = train[:j]
        bk_train = train[j:]
        new_train = new_train + rename(pre_train, bk_train)
    print(len(list(set(new_train))))

Submission status: Accepted

Solution code:
m = int(input())

for i in range(m):
    a = str(input())
    result=[]
    result.append(a)
    
    for j in range(len(a)):
        m=a[:j]
        mre=m[::-1]
        u=a[j:]
        ure=u[::-1]
        
        result.append(m+u)
        result.append(m+ure)
        result.append(mre+u)
        result.append(mre+ure)
        result.append(u+m)
        result.append(u+mre)
        result.append(ure+m)
        result.append(ure+mre)
    print(len(set(result)))

Submission status: Accepted

Solution code:
n= int(input())
for _ in range(n):
    a= input()
    ans=set()
    ans.add(a)
    for i in range(len(a)):
        s=a[:i]
        t=a[i:]
        ans.add(s[::-1]+t[::-1])
        ans.add(t[::-1]+s[::-1])
        ans.add(s[::-1]+t)
        ans.add(t[::-1]+s)
        ans.add(s+t[::-1])
        ans.add(t+s[::-1])
        ans.add(t+s)
        
    print(len(ans))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def solve(S):
    before = set()
    for i in range(1,len(S)):
        a,b = S[:i],S[i:]
        ab = (a,a[::-1])
        cd = (b,b[::-1])
        for j in range(2):
            for k in range(2):
                before.add(ab[j] + cd[k])
                before.add(cd[k] + ab[j])
    print(len(before))

def main():
    M = int(input())
    for _ in range(M):
        S = input()
        solve(S)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
m=int(input())
for i in range(m):
  s=input()
  ptn=set()
  ptn.add(s)
  for j in range(len(s)):
    l=s[:j]
    r=s[j:]
    l_r=l[::-1]
    r_r=r[::-1]
    ptn.add(l+r_r)
    ptn.add(l_r+r)
    ptn.add(l_r+r_r)
    ptn.add(r+l)
    ptn.add(r+l_r)
    ptn.add(r_r+l)
    ptn.add(r_r+l_r)
  print(len(ptn))

Submission status: Accepted

Solution code:
# ライブラリのインポート
#import re #正規表現
import sys
#import heapq
#import collections
#import math

def main():
    for x in sys.stdin:
        x = x.strip("\n")
        ans = set()
        try:
            int(x)
            continue
        except:
            for i in range(1,len(x)):
                f, e = x[:i], x[i:]
                fr = f[::-1]
                er = e[::-1]
                ans = ans.union(set([x, f+er, fr+e, fr+er, e+f,er+f, e+fr, er+fr]))
            print(len(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#頭から分ける場合とけつから分ける場合を考えよう

"""メモ
1142

なんかあってそう
漏れがある
#######################################
blkA_train = arr_train[0:3] 
blkB_train = arr_train[3:4]

abcd
4
4
abc
d

all_list = ['abcd', 'cbad', 'dabc', 'dabc'] setなし　

######################################
blkA_train = arr_train[0:2]
blkB_train = arr_train[2:5]

abcd
4
4
ab
cd

alllist = ['abcd', 'bacd', 'cdab', 'dcab'] setなし

#####################################
blkA_train = arr_train[0:1]
blkB_train = arr_train[1:4]
abcd
4
4
a
bcd

['abcd', 'abcd', 'bcda', 'dcba'] setなし
######################################


"""

arr_train = []
m = input() #kは繰り返す回数
m = int(m)
while(True):
	
	if m == 0:
		break
	x = input()
	arr_train = str(x)
	#arr_train.append(x) #多分appendする必要ない
	
	counter = 0
	"""
	print(len(x)) #len(x) - 1 = n-1 みたいな感じでループ
	print(len(arr_train))
	"""
	
	#blkA_train = [] #最初の分割　これを真ん中のindexの要素を中心に反転しようかな
	#blkB_train = [] #分割されたもの　あとで反転したリストと連結
	
	all_list = []
	
	#文字分割にはリストは必要ない。多分str変数を用意しておいて、ループごとに初期化する。処理終了、連結したstrをリストに保存
	#リストは多分1つだけでよく最後にset+lenで数え上げ
	
	#blkA_train  = None
	#blkB_train  = None
	
	#right = 0 #列車の分割の右分け数
	#left = 0 #列車の分割の左分け数
	
	left = len(arr_train) -1 # まず n-1
	right = 1 #まず1
	
	"""
	blkA_train = arr_train[0:3] #スライスで文字列を指定 このindexを一般化 i j len 左スライスは0で良い 右スライスはleftでいいんじゃない?
	blkB_train = arr_train[3:4] #右スライスは増加していく ループ変数を使え
	"""
	"""
	blkA_train = arr_train[0:2]
	blkB_train = arr_train[2:5]
	"""
	
	"""
	blkA_train = arr_train[0:1]
	blkB_train = arr_train[1:4]
	"""
	
	#反転するものとそうでないもので1ループごとに2つの文字列をリストにappend
	
	"""
	#頭が反転の場合
	all_list.append(blkA_train+blkB_train) #そのまま連結
	all_list.append(blkA_train[::-1]+blkB_train) #反転して連結 ok
	
	#けつが反転の場合
	all_list.append(blkB_train+blkA_train)
	all_list.append(blkB_train[::-1]+blkA_train)
	
	#頭、けつが反転する　頭から連結
	
	all_list.append(blkA_train[::-1]+blkB_train[::-1])
	all_list.append(blkB_train[::-1]+blkA_train[::-1])
	
	all_list.append(blkA_train+blkB_train[::-1])
	all_list.append(blkB_train+blkA_train[::-1])
	"""
	
	while(True):
		blkA_train  = None
		blkB_train  = None
		
		blkA_train = arr_train[0:left]
		blkB_train = arr_train[left:len(arr_train)-1+left]
		
		all_list.append(blkA_train+blkB_train) #そのまま連結
		all_list.append(blkA_train[::-1]+blkB_train) #反転して連結 ok
		
		all_list.append(blkB_train+blkA_train)
		all_list.append(blkB_train[::-1]+blkA_train)
		
		all_list.append(blkA_train[::-1]+blkB_train[::-1])
		all_list.append(blkB_train[::-1]+blkA_train[::-1])
	
		all_list.append(blkA_train+blkB_train[::-1])
		all_list.append(blkB_train+blkA_train[::-1])
		if left == 1: #左分割フラグが1になったら終了
			break
		left -= 1
		#right += 1
	all_list = set(all_list)
	all_list = list(all_list)
	print(len(all_list))
	m -=1

"""
print(blkA_train)
print(blkB_train)
"""

"""
print()
print(all_list)
all_list = set(all_list)
all_list = list(all_list)

print(all_list)
print(len(all_list))
"""
"""
print(blkA_train)
print(blkB_train)

print()
print(all_list)
print()
all_list = set(all_list)
all_list = list(all_list)

print(all_list)
"""

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    s = input()
    m = len(s)

    ans = set()
    for j in range(1, m):
        head = s[0:j]
        tail = s[j:m]

        ans.add(head + tail)
        ans.add(tail + head)

        head = head[::-1]
        ans.add(head + tail)
        ans.add(tail + head)

        tail = tail[::-1]
        ans.add(head + tail)
        ans.add(tail + head)

        head = head[::-1]
        ans.add(head + tail)
        ans.add(tail + head)

    print(len(ans))

Submission status: Accepted

Solution code:
def solve(s):
    ret = [s]
    
    for i in range(len(s)+1):
        ret.append(s[:i]+s[i:][::-1])
        ret.append(s[:i][::-1]+s[i:])
        ret.append(s[:i][::-1]+s[i:][::-1])
        ret.append(s[i:]+s[:i])
        ret.append(s[i:][::-1]+s[:i])
        ret.append(s[i:]+s[:i][::-1])
        ret.append(s[i:][::-1]+s[:i][::-1])
    
    return len(set(ret))

def main():
    ans = []
    m = int(input())
    for i in range(m):
        ans.append(solve(input()))
    for i in ans:
        print(i)

main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

def count(train):
    s = set()
    for i in range(len(train)):
        head = train[0:i+1]
        tail = train[i+1:]
        s.add(head + tail)
        s.add(head + tail[::-1])
        s.add(head[::-1] + tail)
        s.add(head[::-1] + tail[::-1])
        s.add(tail + head)
        s.add(tail + head[::-1])
        s.add(tail[::-1] + head)
        s.add(tail[::-1] + head[::-1])
    return len(s)

def main():
    n = int(input())
    for _ in range(n):
        print(count(input()))

main()