最大子串行 最長公共子串 最長公共子串行

2021-08-31 17:05:21 字數 1407 閱讀 6778

最大子串行

最大子串行是要找出由數組成的一維陣列中和最大的連續子串行。比如的最大子串行就是 ,它的和是8,達到最大;而 的最大子串行是,它的和是6。你已經看出來了,找最大子串行的方法很簡單,只要前i項的和還沒有小於0那麼子串行就一直向後擴充套件,否則丟棄之前的子串行開始新的子串行,同時我們要記下各個子串行的和,最後找到和最大的子串行。

**如下:

#include

using namespace std;

int maxsubseq(const int *arr,int len,int *start,int *end)

if(sum<=0)

}return max;

}int main();

int start,end;

int max=maxsubseq(arr,6,&start,&end);

cout<<"the maxsubseq is from position "<

#include

#include

using namespace std;

//str1為橫向,str2這縱向

const string lcs(const string& str1,const string& str2)} }

// 16

if(c>*max)

20return res;21}

22//呼叫此函式時請注意把較長的字串賦給str1,這主要是為了在回溯最長子序列時節省時間。如果沒有把較長的字串賦給str1不影響程式的正確執行。

23string lcs(const string &str1,const string &str2)

57else if(arr[i][j].second==left)

60else if(arr[i][j].second==up)63}

64string res="";

65while(!st.empty())

70return res;71}

72int main()

22else if(num[s1position][s2position-1]>=num[s1position-1][s2position])

25else28}

29collections.reverse(result);

30return result;31}

std::endl是乙個特殊值,稱為操縱符(manipulator),將它寫入輸出流時具有輸出換行的效果,並重新整理與裝置相關聯的緩衝區(buffer)。通過重新整理緩衝區使用者可立即看到寫入到流中的輸出。

我在除錯以上**時在45行(cout<

最長公共子串行 最長公共子串

1 最長公共子串行 採用動態規劃的思想,用乙個陣列dp i j 記錄a字串中i 1位置到b字串中j 1位置的最長公共子串行,若a i 1 b j 1 那麼dp i j dp i 1 j 1 1,若不相同,那麼dp i j 就是dp i 1 j 和dp i j 1 中的較大者。class lcs el...

最長公共子串行 最長公共子串

1.區別 找兩個字串的最長公共子串,這個子串要求在原字串中是連續的。而最長公共子串行則並不要求連續。2 最長公共子串 其實這是乙個序貫決策問題,可以用動態規劃來求解。我們採用乙個二維矩陣來記錄中間的結果。這個二維矩陣怎麼構造呢?直接舉個例子吧 bab 和 caba 當然我們現在一眼就可以看出來最長公...

最長公共子串 最長公共子串行

子串要求連續 子串行不要求連續 之前的做法是dp求子序列 include include include using namespace std const int inf 0x3f3f3f3f const int mod 1000000007 string s1,s2 int dp 1010 10...