Dynamic Planning Summary "templates"

Source: Internet
Author: User

Maximum increment sub-sequence

Given a sequence, the length of the oldest sequence is found, which increases the order in which all elements of the subsequence are sorted.

1. To find the longest increment of the subsequence length O (n^2)

intarr[30010],list[30010];intLIS (int*arr,intN//arr[] Storage is an array to be asked{intMax =0;//max is the length of the maximum increment sub-sequence     for(inti =1; I <= N; ++i) List[i] =1;//lis[i] The length of the maximum increment subsequence before I is stored, starting at 1     for(inti =2; I <= N; ++i) for(intj =1; J < I; ++J)all positions before//traverse I            if(Arr[i] >= arr[j] && list[i]<list[j]+1) List[i] = List[j] +1;//arr[i]>arr[j] to increment            //lis[i]<lis[j] + 1 ensures the current longest increment sequence     for(inti =1; I <= N; ++i)if(Max < list[i]) Max = List[i];returnMax;}

2. To find the longest increment of the subsequence length O (nlogn)

intarr[10010],list[10010];intstack[10010];intLIS (int*arr,intN) {inttop =0; Stack[top] =-1; for(inti =1; I <= N; ++i) {if(Arr[i] > Stack[top]) Stack[++top] = Arr[i];Else{intLow =1;intHigh = top; while(Low <= High) {int Mid= (low + high)/2;if(Arr[i] > stack[Mid]) Low =Mid+1;ElseHigh =Mid-1;        } Stack[low] = Arr[i]; }} return top;}
Longest common sub-sequence

Given two sequences, find the length of the oldest sequence that occurs simultaneously in two sequences. A subsequence is a sequence that appears in a relative order, but not necessarily continuous.

1. Find the longest common sub-sequence length

Chars1[ -],s2[ -];intdp[ -][ -];//Find common sub-sequences of string S1 and string S2intLCsChar*S1,Char*S2) {intLen1 =strlen(S1);intLen2 =strlen(S2); for(inti =0; I <= len1; ++i) { for(intj =0; J <= Len2; ++J) {if(i = =0|| j = =0) Dp[i][j] =0;Else if(s1[i-1] = = s2[j-1]) Dp[i][j] = dp[i-1][j-1] +1;ElseDP[I][J] = max (dp[i-1][j],dp[i][j-1]); }    }returnDP[LEN1][LEN2];}

2. Find the longest common subsequence length, and output the path

intdp[ the][ the],pre[ the][ the],len1,len2;Chars1[ the],s2[ the];voidLCS (Char*S1,Char*S2) { for(inti =0; I <= len1; ++i) pre[i][0] =1; for(inti =0; I <= len2; ++i) pre[0][i] =2;//Get the longest common subsequence and mark the previous state of Dp[i][j], used to backtrack to find the path     for(inti =0; I <= len1; ++i) { for(intj =0; J <= Len2; ++J) {if(i = =0|| j = =0) Dp[i][j] =0;if(s1[i-1] = = s2[j-1]) {Dp[i][j] = dp[i-1][j-1] +1; PRE[I][J] =0; }Else if(dp[i-1][J] >= dp[i][j-1]) {Dp[i][j] = dp[i-1][J]; PRE[I][J] =1; }Else{Dp[i][j] = dp[i][j-1]; PRE[I][J] =2; }        }    }}voidPrint (intIintJ//Backtracking output a new string sequence{if(i = =0&& J = =0)return;if(Pre[i][j] = =0) {Print (I-1, J-1);printf("%c", s1[i-1]); }Else if(Pre[i][j] = =1) {Print (I-1, j);printf("%c", s1[i-1]); }Else{Print (i,j-1);printf("%c", s2[j-1]); }}voidSolveChar*S1,Char*S2) {len1 =strlen(S1); Len2 =strlen(S2);    LCS (S1,S2); Print (LEN1,LEN2);printf("\ n");}
Longest palindrome subsequence

Give a string to find out its longest palindrome subsequence of the length of LPs. For example, if the given sequence is "Bbabcbcab", then the output should be 7, "Babcbab" is the longest palindrome subsequence in it.

Char S[2020];int dp[2020][2020];//dp[I][J] represents s[i~j] longest palindrome subsequence int LPS (char *s) {memset (Dp,0,sizeof (DP));int len = strlen (s);for (int i = len-1; I >= 0; i.)    {Dp[i][i] = 1;For (int j = i+1; j < Len; ++j)        {if (s[i] = = S[j])//head and tail of the same, the longest palindrome sequence for the end of the part LPs plus heads and tailsDp[i][j] = dp[i+1][j-1] + 2;else//head and tail different, longest palindrome subsequence is to go to the first part of LPs and go to the tail part of LPs longerDp[i][j] = max (dp[i][j-1],dp[i+1][j]);        }    }return dp[0][len-1];}
Minimum editing distance

Given a length of two strings of M and N, there are several operations: replace (R), insert (I), and delete (D), all of which are the same operation. Find the minimum (number of operations) to convert one string into another that needs to be modified.

intdp[1010][1010],len1,len2;Chars1[1010],s2[1010];intEditdist (Char*S1,Char*S2) {intLen1 =strlen(S1);intLen2 =strlen(S2);//When the size of the two string is 0, its operating distance is 0. //When the length of one of the strings is zero, the required operating distance is the length of the other string.     for(inti =0; I <= len1; ++i) dp[i][0] = i; for(inti =0; I <= len2; ++i) dp[0][i] = i; for(inti =1; I <= len1; ++i) { for(intj =1; J <= Len2; ++J) {if(s1[i-1] = = s2[j-1])//Align S1[i-1] and s2[j-1], no need to changeDP[I][J] = dp[i-1][j-1];ElseDp[i][j] = min (dp[i-1][j],min (dp[i][j-1],dp[i-1][j-1])) +1;//S1 Prefix right-aligned, S2 prefix right ", delete S1-i-character--dp[i-1][j]            //S2 Prefix right-aligned, S1 prefix right ", delete S2 J-character-dp[i][j-1]            //S1 Prefix right-aligned, S2 prefix right-aligned, I, J not the same, replace with Dp[i-1][j-1]}    }returnDP[LEN1][LEN2];}

Dynamic Planning Summary "templates"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.