def gS(k):#k is a list of strings that are going to be combined to new strings
s = ""
for i in k:
s += i
result = [""]
g = 0
for i in s:
if i not in result[g]:
result[g] += str(i)
else:
result.append("")
g += 1
result[g] += str(i)
return result
def bS(k): #only when situation like kabba occurs, kab ba --> ka b ba --> k ab ba -bS-> k a b ba --> k a b b a
return [k[:-1], k[-1]] #returns a list
def dS(s,t): #transfers the last char of left string to right
return [s[:-1], s[-1]+t]
def case1(lista, iS):
x = dS(lista[-(iS+1)], lista[-iS])
lista[-(iS+1)] = x[0]
lista[-iS] = x[1]
return lista
def case2(lista, iS):
x = [lista[-(iS+1)][-1]]
x += lista[-(iS):]
x = gS(x)
del lista[-(iS):]
lista[-1] = lista[-1][:-1] #removing the last character
lista += x
return lista
def case3(lista, iS):
#we go back the elements in lista until we find an element that is NOT singular
d = 1 #the amount of steps we need to go back until we find an element that is not singular
while len(lista[-(iS+d)]) == 1:
d += 1
x = [lista[-(iS+d)][-1]]
x += lista[-(iS+d-1):]
x = gS(x)
del lista[-(iS+d-1):]
print(lista, iS, d)
lista[-1] = lista[-1][:-1]
lista += x
return lista
def getIs(lista):
for iS in range(1,len(lista)+1):
if iS == len(lista):
return iS
if lista[-(iS+1)][-1] not in lista[-iS]:
return iS
def tails(lista, iS):
pii = 1
for i in range(1, iS+1):
pii *= pow(2, len(lista[-(i)])-1)
return pii
def calc(n, s):
lista = gS(s)
print(lista)
if len(lista) == 1:
return pow(2, len(lista)-1)
summa = pow(2, len(lista[-1]))
iS = 1 #how many "tails" do we have, e.g. ka k ka --> 2 tails "k" and "ka", we only manipulate the left-most tail, thats why we keep count of
while len(lista) > iS: #DOES NOT TAKE INTO CONSIDERATION WHETHER THE LIST IS ACTUALLY JUST 1 CHAR LONG!!! HAS TO BE DONE IN A RATHER DIRTY WAY
#possibility 1: we can expand the last element of the list, i.e. the second last element's last element is not in the last element AND the second last element is not singular
if lista[-(iS+1)][-1] not in lista[-iS] and len(lista[-(iS+1)]) != 1:
lista = case1(lista, iS)
print("case1:", lista)
#possibility 2: we cannot expand the last element of the list, i.e. the second last element's last element is in the last element AND the second last element is not singular
elif lista[-(iS+1)][-1] in lista[-iS] and len(lista[-(iS+1)]) != 1:
lista = case2(lista, iS)
print("case2:", lista)
#possibility 3: we cannot expand the last element of the list, i.e. the second last element's last element is in the last element AND the second last element IS singular
elif lista[-(iS+1)][-1] in lista[-iS] and len(lista[-(iS+1)]) == 1:
case3(lista, iS)
print("case3:", lista)
if len(lista[0]) == 1: #remove all the elements from the left that are singular, important for poss. 3
while len(lista[0]) == 1:
lista.pop(0)
iS = getIs(lista)
summa += tails(lista, iS)
print(summa)
x = str(input())
calc(3, x)