def equilibreGlouton(words, textEquilibre, n):
    if(words == []):
        return textEquilibre
    else:
        if(textEquilibre == []):
            textEquilibre = [[words[0][:]]]
            words.pop(0)
            return equilibreGlouton(words, textEquilibre, n)
        
        textWithTransfer = textEquilibre[:]
        textWithTransfer.append([words[0][:]])
        print "WT", textWithTransfer

        if(possibleString(textEquilibre[-1] + [words[0]], n)):
            lastString = textEquilibre[-1][:] + [words[0][:]]
            textWoTransfer = textEquilibre[:]
            textWoTransfer.pop(-1)
            textWoTransfer.append(lastString)
            words.pop(0)
            print "WoT", textWoTransfer
            
            maxMinWoT = longShortDistance(textWoTransfer)
            distanceWoT = maxMinWoT[0]-maxMinWoT[1]
            maxMinWT = longShortDistance(textWithTransfer)
            distanceWT = maxMinWT[0]-maxMinWT[1]
            if(distanceWoT < distanceWT):
                return equilibreGlouton(words, textWoTransfer, n)
            else:
                return equilibreGlouton(words, textWithTransfer, n)
        else:
            words.pop(0)
            return equilibreGlouton(words, textWithTransfer, n)
def bruteForceWoTransition(words, text, measure, strl):
    if(words == []):
        return [],text,measure,strl
    else:
        wordToJoin = words[0][:]
        lastString = text[-1][:]
        lastString.append(wordToJoin)
        if(possibleString(lastString , strl)):
            text.pop(-1)
            text.append(lastString)
            newText = text[:]
            newText2 = text[:]
            newMeasure = measureOfTextEquilibrium(newText, strl)
            newWords = words[:]
            if(type(newWords) is str):
                newWords = []
            else:
                newWords.pop(0)
            
            a = bruteForceWithTransition(newWords, newText, newMeasure, strl)
            b = bruteForceWoTransition(newWords, newText2, newMeasure, strl)
            
            if(a[2] < b[2]):
                return a
            else:
                return b
        else:
            return [],[],sys.maxint,strl
def fusionTextes(text1, text2, textm1, textm2, stringLength): #O(1)
    fusionedString = text1[-1][:] + text2[0][:] #O(1)
    if(possibleString(fusionedString, stringLength)):
        t1 = text1[:] #O(1)
        t2 = text2[:] #O(1)
        fm = blancLigne(fusionedString ,stringLength)
        m1 = blancLigne(text1[-1], stringLength)
        m2 = blancLigne(text2[0], stringLength)
        newM = (textm1 - m1) + (textm2 - m2) + fm
        
        if(len(text1)>0):
            text1.pop() #O(1)
        if(len(text2)>0):
            text2.pop(0) #O(1)
        resultText = text1 + [fusionedString] + text2 #O(1)

        return True, newM, resultText #O(1)
    else:
        return False,0,[]
def fusionTextsMinMax(text1, text2, stringLength):
    #print "Try to fusion"
    fusionedString = text1[-1][:] + text2[0][:]
    if(possibleString(fusionedString, stringLength)):
        #print "Fusioned string: ", fusionedString
        #print "is possible"
        fusionedLength = longueur(fusionedString)
        #print "with length", fusionedLength
        if(len(text1)>0):
            text1.pop()
        if(len(text2)>0):
            text2.pop(0)
    
        resultText = text1 + [fusionedString] + text2
        #print "minmax for fusioned text:", longShortDistance(resultText)


        return True, longShortDistance(resultText), resultText
    else:
        #print "Fusion impossible"
        return False,[0,0],[]
def equilibreGloutonBl(words, textEquilibre, n):
    if(words == []):
        return textEquilibre
    else:
        if(textEquilibre == []):
            textEquilibre = [[words[0][:]]]
            words.pop(0)
            
            return equilibreGloutonBl(words, textEquilibre, n)
        
        if(possibleString(textEquilibre[-1] + [words[0]], n)):
            lastString = textEquilibre[-1][:] + [words[0][:]]
            textEquilibre.pop(-1)
            textEquilibre.append(lastString)
            words.pop(0)
            
            return equilibreGloutonBl(words, textEquilibre, n)
        else:
            lastString = [words[0][:]]
            words.pop(0)
            textEquilibre = textEquilibre[:]
            textEquilibre.append(lastString)
            
            return equilibreGloutonBl(words, textEquilibre, n)