Simple DP with a vulnerability

Source: Internet
Author: User

Question Portal: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? Id = 18201

In fact, it is not difficult, but after a long time, I found that the original question was not clearly read, attention, the original sequence was too pitfall,

I have seen that many others use the longest common subsequence, but I use the longest ascending subsequence to map the original sequence to an ascending sequence, the data for this question exactly meets this condition.

For example, if the correct sequence is $5 \ 6 \ 4 \ 1 \ 3 \ 2 $, I can map it

$ Id [5] \ rightarrow 1 $ id [6] \ rightarrow 2 $ id [4] \ rightarrow 3 $

$ Id [1] \ rightarrow 4 $ id [3] \ rightarrow 5 $ id [2] \ rightarrow 6 $

For another sequence, such as $6 \ 1 \ 3 \ 2 \ 5 \ 4 $

Then I can map it to $2 \ 4 \ 5 \ 6 \ 1 \ 3 $ to find the longest ascending subsequence of the sequence.

Directly paste the Code:

#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int maxn = 30;int ID[maxn];int b[maxn];int ans1,ans2;int a[maxn];int c[maxn];int n;int opt[maxn];int solve(){    int MAX = 0;    a[0] = -100;    memset(opt,0,sizeof(opt));    for(int i = 1;i <= n;++i)    for(int j = 0;j <= i-1;++j){        if(a[j] <= a[i])            opt[i] = max(opt[i],opt[j] + 1);    }    for(int i = 1;i <= n;++i){        MAX = max(MAX,opt[i]);    }    return MAX;}void print(){    for(int i = 1;i <= n;++i)        cout<<a[i]<<" ";    cout<<endl;}int getid(int index){    for(int i = 1;i <= n;++i)        if(ID[i] == index)        return i;}int main(){    //freopen("in.txt","r",stdin);    int fuck;    while(~scanf("%d",&n)){        for(int i = 1;i <= n;++i){            scanf("%d",&fuck);            ID[fuck] = i;        }        while(1){            ans1 = 0;            for(int i = 1;i <= n;++i){                if(scanf("%d",&fuck)==EOF) goto holly_shit;                a[fuck] = getid(i);            }            printf("%d\n",solve());            memset(a,0,sizeof(a));        }        holly_shit: continue;    }    return 0;}

Simple DP with a vulnerability

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.