UV 111 History Grading

Source: Internet
Author: User

UVA_111

This question is not difficult, but it hurts to understand it.

The question indicates the longest common subsequence of two event sequences, and the question indicates the time when each event occurs, therefore, you must first locate the sequence of events based on the time when each event occurs.

For example, if the input is 1 3 4 2, the actual event sequence is 1 4 2 3.

#include<stdio.h>
#include<string.h>
#define MAXD 30
int N, a[MAXD], b[MAXD], f[MAXD][MAXD];
int init()
{
int i, k;
for(i = 1; i <= N; i ++)
{
if(scanf("%d", &k) != 1)
return 0;
b[k] = i;
}
return 1;
}
void solve()
{
int i, j;
memset(f, 0, sizeof(f));
for(i = 1; i <= N; i ++)
for(j = 1; j <= N; j ++)
{
f[i][j] = f[i - 1][j];
if(f[i][j - 1] > f[i][j])
f[i][j] = f[i][j - 1];
if(a[i] == b[j] && f[i - 1][j - 1] + 1 > f[i][j])
f[i][j] = f[i - 1][j - 1] + 1;
}
printf("%d\n", f[N][N]);
}
int main()
{
int i, k;
scanf("%d", &N);
for(i = 1; i <= N; i ++)
{
scanf("%d", &k);
a[k] = i;
}
while(init())
solve();
return 0;
}


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.