#Srivani Edupuganti
#February 3, 2020
#Senior 5
#Enloe High School
#Potter
'''
algorithm: recursive function. define the things in a function.
    function takes 3 arguments: the current ADF, string 1, string 2
    second function to find largest string? maybe not needed, because you
    would only need it once in the first function.

    first function:
        find longest common string (return it from a function)
        splice: left1, right1, left2, right2
            splicing will be based on find(common), and len(common)
        function(left1, left2, adf)
        adf +=len(common)
        function(right2, left2)
        ENDCASES: len(common) = 0, len of spliced string = 0.
            if len(common) > 0, then continue recursion
                if len left1 >0 and len left2>1, etc etc
    IMPORTANT NOTE:::: MUST REMOVE ALL NON ALPHABET CHARACTERS FROM STRING
        
'''
import re
regex = re.compile('[^a-zA-Z]')
#First parameter is the replacement, second parameter is your input string
#EXAMPLE:: regex.sub('', 'ab3d*E')

def longcommon(first, second):
    sizelc = 1
    lc = ['']
    pos = 0
    while (pos+sizelc) <= len(first):
        teststring = first[pos:pos+sizelc]
        if (first.find(teststring)!=-1) and (second.find(teststring)!=-1): #test is in both strings
            if sizelc > len(lc[0]):
                lc = [teststring]
                sizelc+=1
            elif sizelc == len(lc[0]):
                if not(teststring in lc):
                    lc.append(teststring)
                sizelc+=1
        else: #test is not in both strings, therefore not common
            sizelc = len(lc[0])
            pos+=1
    #print(lc)
    if len(lc)!=1:
        toret = lc[0]
        for x in range(len(lc)-1):#range is noninclusive, so it works the way a for loop does.
                                            #-1 so we can test against +1
            if lc[x+1]0 and len(left2)>0:
            print("ADF BEFORE LEFT: " +str(ADF))
            ADF += ADFrecur(left1, left2, ADF)
            print("ADF AFTER LEFT: " + str(ADF))
        ADF += len(comstring)
        print (ADF)
        print ("ADF AFTER MID: " +str(ADF))
        if len(right1)>0 and len(right2)>0:
            print("ADF BEFORE RIGHT: " + str(ADF))
            ADF += ADFrecur(right1, right2, ADF)
            print("ADF AFTER RIGHT: " +str(ADF))
    return ADF
'''
def ADFrecur(first, second, ADF):
    comstring = longcommon(first, second)
    if len(comstring)!=0:
        left1 = first[:first.find(comstring)]
        right1 = first[first.find(comstring)+len(comstring):]
        left2 = second[:second.find(comstring)]
        right2 = second[second.find(comstring)+len(comstring):]

        if len(left1)>0 and len(left2)>0:
            templeftadf = 0
            ADF += ADFrecur(left1, left2, templeftadf)
        ADF += len(comstring)
        #print (ADF)
        if len(right1)>0 and len(right2)>0:
            temprightadf = 0
            ADF += ADFrecur(right1, right2, temprightadf)
    return ADF

filename = 'E:/senior1test.txts.txt'
with open(filename) as file_object:
    lines = file_object.readlines()
while len(lines) > 0:
    ADF = 0
    string1 = lines.pop(0).rstrip()
    string1 = regex.sub('', string1).upper() #removing non-alphabetic
    string2 = lines.pop(0).rstrip()
    string2 = regex.sub('', string2).upper() #removing non-alphabetic
    ADF+=ADFrecur(string1, string2, ADF)
    print(ADF)

    

'''
test = "0123456"
print(test[:6])
print(test[:])
print(test[0:])
print(test[:7])
print(test[:8])
print(test[6:])
print(test[7:])
print(test[:0])
print(test[8:])
newtest = test[:2] + test[2+1:]
print (newtest)
newtest = test[:0] + test[1:]
print(newtest)
newtest = test[:7]+ test[8:]
print(newtest)
newtest = test[:0]
print(len(newtest))
'''