! HDU 1243 Counter-terrorism training camp--dp--(longest common sub-sequence)

Source: Internet
Author: User

Test instructions: give you a bullet sequence and a terrorist sequence, only the bullets hit the corresponding serial number of terrorists to score, each terrorist has a corresponding score, to seek the maximum score

Analysis: The abstract model is the longest common sub-sequence, the direct set of formula: Dp[i][j] for the first time to hit J terrorist with the maximum score, note that this refers to the process from the starting state to the optimal solution, rather than the optimal solution of this state; State transfer: 1. When a[i ]==B[J], Dp[i][j]=dp[i-1][j-1]+v,v is the score of the terrorist b[j]; 2. Otherwise, Dp[i][j]=max (Dp[i-1][j-1],dp[i-1][j],dp[i][j-1])

Note:each time the 1.DP array is saved is the optimal solution of the process from the start state to the current state, not the optimal solution of this state; Remember that DP is a process, not an instantaneous one .

The error occurred in this question: 1. RE. The question did not say the data range I guess 1000, and later found it should be 2000; but I made a stupid mistake is I set the dp[1000][1000], should not be dp[1005][1005] it!

2.TLE. There are two reasons, one is the state transfer did not understand the middle once wrote a triple cycle; second, the problem can not be used memset, the use of the time-out.

3.MLE. The original is I for fear of WA, set a long long dp[][], the result is changed to int dp[][] on the line

Remember these few wonderful reasons for the wrong!

Code:

#include <iostream> #include <algorithm> #include <cstring> #include <string> #define INF 1000000007using namespace Std;int n,sc[200];int dp[2007][2007];string a,b;char c[10000];int n1,n2;int max (int p,int q) { C0/>return P>q?p:q;} void DP () {for    (int i=0;i<=n1;i++) dp[i][0]=0;    for (int j=0;j<=n2;j++) dp[0][j]=0;    for (int i=1;i<=n1;i++) {for        (int j=1;j<=n2;j++) {            if (a[i-1]==b[j-1])               dp[i][j]=dp[i-1][j-1]+sc[a[ I-1]];            Else Dp[i][j]=max (Dp[i-1][j-1],max (dp[i-1][j],dp[i][j-1));}}}            int main () {    int tmp;    while (Cin>>n) {for        (int i=0;i<200;i++) sc[i]=0;        for (int i=0;i<n;i++) cin>>c[i];        for (int i=0;i<n;i++) {            cin>>tmp;            sc[c[i]]=tmp;        }        cin>>a>>b;        N1=a.length ();        N2=b.length ();        DP ();        cout<<dp[n1][n2]<<endl;    }}


! HDU 1243 Counter-terrorism training camp--dp--(longest common sub-sequence)

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.