Longest dp public sub-sequence (ultraviolet) 111-History Grading

Source: Internet
Author: User

Give the correct time table for 1-N events and the time table for 1-N events to calculate the maximum number of events, which is the same as the correct order. Solution: Sort Events in order of ranking, and then find the length of the longest common subsequence. Dp [I] [j] = dp [I-1] [J-1] + 1 (save1 [I] = save [j]) dp [I] [j] = max (dp [I-1] [j], dp [I] [J-1]); Code: # include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <stack> # include <queue> # define eps 1e-6 # define INF (1 <20) # define PI acos (-1.0) using namespace std; int save1 [30], save2 [30]; int dp [30] [30]; int main () {int n, temp; Scanf ("% d", & n); for (int I = 1; I <= n; I ++) {scanf ("% d", & temp ); save1 [temp] = I; // sort events in the ranking order so that the longest common subsequences have the same rank.} while (scanf ("% d ", & temp )! = EOF) {save2 [temp] = 1; for (int I = 2; I <= n; I ++) {scanf ("% d", & temp ); save2 [temp] = I;} www.2cto.com memset (dp, 0, sizeof (dp); for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) {if (save1 [I] = save2 [j]) dp [I] [j] = dp [I-1] [J-1] + 1; // max (dp [I-1] [J-1] + 1, dp [I-1] [j]), dp [I] [J-1]); else dp [I] [j] = max (dp [I-1] [j], dp [I] [J-1]);} printf ("% d \ n", dp [n] [n]);} return 0 ;}

Related Article

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.