已有用c語言實現程式碼:
先羅列一下碰到的幾處,C語言與python同一語義的不同實現方法:
字串長度:
C語言: int strTotalLen = strlen(s);python: strTatalLen = len(s)
二維陣列定義並初始化:
C語言: bool dp[strTotalLen][strTotalLen]; for (int k = 0; k < strTotalLen; k++) { for (int q = 0; q < strTotalLen; q++) { dp[k][q] = 0; } }python:import numpy as npdp = np.zeros((strTatalLen, strTatalLen))
for迴圈:
C語言: for (int strLen = 0; strLen < strTotalLen; strLen++) { for (int strStart = 0; strStart + strLen < strTotalLen; strStart++) { int strEnd = strStart + strLen; python: for strLen in range(strTatalLen): for strStart in range(strTatalLen): strEnd = strStart + strLen if strEnd >= strTatalLen: break
邏輯關係與:
C語言: dp[strStart][strEnd] = (s[strStart] == s[strEnd] && dp[strStart + 1][strEnd - 1]);Python: dp[strStart][strEnd] = (s[strStart] == s[strEnd] and dp[strStart + 1][strEnd - 1])
c語言完整程式碼:
#include<stdio.h>#include<stdlib.h>#include<string.h>char* longestPalindrome_dp(char* s){ int strTotalLen = strlen(s); //定義並初始化二維陣列 //DP關鍵步驟2:緩衝並複用以往結果 bool dp[strTotalLen][strTotalLen]; for (int k = 0; k < strTotalLen; k++) { for (int q = 0; q < strTotalLen; q++) { dp[k][q] = 0; } } //儲存最終結果 char* retStr = (char*)malloc(sizeof(int) * strTotalLen); memset(retStr, 0x00, sizeof(char) * strTotalLen); //DP關鍵步驟3:按照順序從小往大算 for (int strLen = 0; strLen < strTotalLen; strLen++)//迴文子串的長度 { for (int strStart = 0; strStart + strLen < strTotalLen; strStart++) {//迴文子串的開始位置strStart,這樣可以透過strEnd = strStart + strLen得到子串的結束位置 int strEnd = strStart + strLen; if (strLen == 0)//邊界條件1,長度為1的子串,顯然是迴文子串 { dp[strStart][strEnd] = 1; } else if (strLen == 1)//邊界條件2,長度為2的子串,只要它的兩個字母相同,是迴文子串 { dp[strStart][strEnd] = (s[strStart] == s[strEnd]); } else//DP關鍵步驟1:狀態轉移方程,s[strStart + 1][strEnd - 1]是迴文子串 //且s[strStart] == s[strEnd]時,s[strStart][strEnd]才是迴文子串 { dp[strStart][strEnd] = (s[strStart] == s[strEnd] && dp[strStart + 1][strEnd - 1]); } //如果有更長的迴文子串則更新 if (dp[strStart][strEnd] && strLen + 1 > strlen(retStr)) { for (int m = strStart,n = 0; n < strLen + 1; m++,n++) { retStr[n] = s[m]; printf("retStr[%d] = %c\n",n,retStr[n]); } retStr[strTotalLen + 1] = '\0'; } } } return retStr;}int main(){ char s[] ="ovvolevel"; char* s1 = longestPalindrome_dp(s); printf("%s\n", s1); return 0;}
python 完整程式碼:
import numpy as npclass Solution: def longestPalindromeDp(self, s: str) -> str: strTatalLen = len(s) # 定義並初始化二維陣列 # DP關鍵步驟二:緩衝並複用以往結果 dp = np.zeros((strTatalLen, strTatalLen)) retStr = "" # 儲存最終結果 # DP關鍵步驟三:按照順序從小往大算 for strLen in range(strTatalLen): # 迴文子串的長度 # 迴文子串的開始位置strStart,這樣可以透過strEnd = strStart + strLen得到子串的結束位置 for strStart in range(strTatalLen): strEnd = strStart + strLen if strEnd >= strTatalLen: break if strLen == 0: # 邊界條件1,長度為1的子串,顯然是迴文子串 dp[strStart][strEnd] = 1 elif strLen == 1: # 邊界條件2,長度為2的子串,只要它的兩個字母相同,是迴文子串 dp[strStart][strEnd] = (s[strStart] == s[strEnd]) else: # DP關鍵步驟1:狀態轉移方程,s[strStart + 1][strEnd - 1]是迴文子串 # 且s[strStart] == s[strEnd]時,s[strStart][strEnd]才是迴文子串 dp[strStart][strEnd] = (s[strStart] == s[strEnd] and dp[strStart + 1][strEnd - 1]) if dp[strStart][strEnd] and strLen + 1 > len(retStr): retStr = s[strStart:strEnd + 1] return retStrif __name__ == "__main__": s = "ovvolevel" test = Solution() s1 = test.longestPalindromeDp(s) print(s1, end=' ')
最新評論