亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有一種方法可以計算鏡像時相同的最長子字符串或子列表

有沒有一種方法可以計算鏡像時相同的最長子字符串或子列表

慕俠2389804 2023-10-26 14:43:16
所以我需要找到可以鏡像的最長子列表,知道元素 ex 的數量:n = 5my_list = [1,2,3,2,1]這是我的代碼:n = int(input())my_list = list(map(int, input().split()))c = 0s1 = my_listx = 0i = 0while i < n:    s2 = s1[i:]    if s2 == s2[::-1]:        if c <= len(s2):            c = len(s2)    if i >= n-1:        i = 0        n = n - 1        s1 = s1[:-1]    i += 1print(c)正如我們所看到的,鏡像時列表是相同的,但是當結果不是預期的n = 10時。my_list = [1,2,3,2,1,332,6597,6416,614,31]35
查看完整描述

2 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

我的解決方案是將每次迭代中的數組拆分為左數組和右數組,然后反轉左數組。接下來,比較每個數組中的每個元素,并在元素相同時將長度變量加一。


def longest_subarr(a):

    longest_exclude = 0

    for i in range(1, len(a) - 1):

        # this excludes a[i] as the root

        left = a[:i][::-1]

        # this also excludes a[i], needs to consider this in calculation later

        right = a[i + 1:] 


        max_length = min(len(left), len(right))

        length = 0


        while(length < max_length and left[length] == right[length]):

            length += 1

        longest_exclude = max(longest_exclude, length)

    # times 2 because the current longest is for the half of the array

    # plus 1 to include to root

    longest_exclude = longest_exclude * 2 + 1


    longest_include = 0

    for i in range(1, len(a)):

        # this excludes a[i] as the root

        left = a[:i][::-1]

        # this includes a[i]

        right = a[i:] 


        max_length = min(len(left), len(right))

        length = 0


        while(length < max_length and left[length] == right[length]):

            length += 1

        longest_include = max(longest_include, length)

    # times 2 because the current longest is for the half of the array

    longest_include *= 2


    return max(longest_exclude, longest_include)

    

print(longest_subarr([1, 4, 3, 5, 3, 4, 1]))

print(longest_subarr([1, 4, 3, 5, 5, 3, 4, 1]))

print(longest_subarr([1, 3, 2, 2, 1]))

這涵蓋了奇數長度子數組[a, b, a]和偶數長度子數組的測試用例[a, b, b, a]。


查看完整回答
反對 回復 2023-10-26
?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

由于您需要可以鏡像的最長序列,因此這里有一個簡單的 O(n^2) 方法。

轉到每個索引,以它為中心,如果數字相等,則向左和向右擴展,一次一步。否則中斷,并移動到下一個索引。


def longest_mirror(my_array): 

    maxLength = 1

  

    start = 0

    length = len(my_array) 

  

    low = 0

    high = 0

  

    # One by one consider every character as center point of mirrored subarray

    for i in range(1, length): 

    # checking for even length subarrays

        low = i - 1

        high = i 

        while low >= 0 and high < length and my_array[low] == my_array[high]: 

            if high - low + 1 > maxLength: 

                start = low 

                maxLength = high - low + 1

            low -= 1

            high += 1

  

        # checking for even length subarrays

        low = i - 1

        high = i + 1

        while low >= 0 and high < length and my_array[low] == my_array[high]: 

            if high - low + 1 > maxLength: 

                start = low 

                maxLength = high - low + 1

            low -= 1

            high += 1

  

    return maxLength


查看完整回答
反對 回復 2023-10-26
  • 2 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號