LCS/LIS/lcis template Summary

Source: Internet
Author: User
/************************* LCS/LIS/lcis template summary: *************************//************* **************************************** LCS: the longest common subsequence calculates the LCS of the sequence a with the length of len1 and the sequence B with the length of len2. Note: The sequence subscript starts to scroll the array writing method from 0. Returns the length of LCS ************************************* * **************/int lcs (INT len1, int len2) {memset (DP, 0, sizeof (DP); For (INT I = 1; I <= len1; I ++) {for (Int J = 1; j <= len2; j ++) {If (S1 [I-1] = S2 [J-1]) DP [I % 2] [J] = DP [(I-1) % 2] [J-1] + 1; else {int M1 = DP [(I-1) % 2] [J]; int m2 = DP [I % 2] [J-1]; DP [I % 2] [J] = max (M1, M2) ;}} return DP [len1% 2] [len2];} /*************************************** * *** LIS: most Long rising sub-sequence HDU 1257 minimum interception system ******************************* * ************/# include <stdio. h ># include <algorithm> using namespace STD; const int maxn = 1000 + 10; const int INF = 30000 + 10; int A [maxn]; // missile height int H [maxn]; // H [I] indicates the current height of the I-th system interception int main () {int N; while (scanf ("% d", & N )! = EOF) {for (INT I = 0; I <n; I ++) {scanf ("% d", & A [I]); // H [I] = inf;} H [0] =-1; // guarantee that the boundary increments H [1] = A [0]; // The first int Len = 1; // The length of the For (INT I = 1; I <n; I ++) {int Index = lower_bound (H, H + Len + 1, a [I])-H; // ensure that H [Index] is the first H [Index] = A [I] in array h; If (index> Len) len = index;} printf ("% d \ n", Len);} return 0 ;} /*************************************** * *********** lcis: the longest common ascending subsequence returns the length of the LCS sequence ************* starting from 1 when the length of sequence a is n and the length of sequence B is M ************* **************************************** /int dp [maxn]; int LCS (int n, int m) {memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) {int TMP = 0; // The maximum DP [J] For (Int J = 1; j <= m; j ++) {if (a [I]> B [J] & DP [J]> TMP) TMP = DP [J]; else if (a [I] = B [J]) DP [J] = TMP + 1 ;}} int ans = 0; For (INT I = 1; I <= m; I ++) ans = max (ANS, DP [I]); Return ans ;}

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.