麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

poj1692 Crossed Matchings(dp,最長(zhǎng)公共子序列變形,好題)

2019-11-14 11:55:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Crossed Matchings
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2838 Accepted: 1840

Description

There are two rows of positive integer numbers. We can draw one line segment between any two equal numbers, with values r, if one of them is located in the first row and the other one is located in the second row. We call this line segment an r-matching segment. The following figure shows a 3-matching and a 2-matching segment. 
We want to find the maximum number of matching segments possible to draw for the given input, such that: 1. Each a-matching segment should cross exactly one b-matching segment, where a != b . 2. No two matching segments can be drawn from a number. For example, the following matchings are not allowed. 
Write a PRogram to compute the maximum number of matching segments for the input data. Note that this number is always even.

Input

The first line of the input is the number M, which is the number of test cases (1 <= M <= 10). Each test case has three lines. The first line contains N1 and N2, the number of integers on the first and the second row respectively. The next line contains N1 integers which are the numbers on the first row. The third line contains N2 integers which are the numbers on the second row. All numbers are positive integers less than 100.

Output

Output should have one separate line for each test case. The maximum number of matching segments for each test case should be written in one separate line.

Sample Input

36 61 3 1 3 1 33 1 3 1 3 14 41 1 3 3 1 1 3 3 12 111 2 3 3 2 4 1 5 1 3 5 103 1 2 3 2 4 12 1 5 5 3 

Sample Output

608

Source

Tehran 1999

參考博客鏈接

題意:

給出兩行數(shù),求上下匹配的最多組數(shù)是多少。匹配規(guī)則1.匹配對(duì)的數(shù)字必須相同2.每個(gè)匹配必須有且只能有一個(gè)匹配與之相交叉,且相交叉的兩組匹配數(shù)字必須不同3.一個(gè)數(shù)最多只能匹配一次

題解:

一開(kāi)始我以為是個(gè)二分匹配的題目,后來(lái)想了好久不知道怎么處理第二個(gè)條件。

這題其實(shí)是動(dòng)態(tài)規(guī)劃題。分析:用dp[i][j]表示第一行取i個(gè)數(shù),第二行取j個(gè)數(shù)字的最多匹配項(xiàng)對(duì)于某個(gè)dp[i][j]:1.不匹配第一行i個(gè),或不匹配第二行第j個(gè):dp[i][j]=Max(dp[i-1][j],dp[i][j-1])2.如果a[i]==b[j],不產(chǎn)生新匹配,匹配結(jié)果為1的值3.若a[i]!=b[j]:a.則第一行從i往前掃,直到掃到第一個(gè)a[k1]==b[j](k1 b.同理,第二行從j往前掃,直到掃到第一個(gè)b[k2]==a[i](k2 若找不到這樣的k1,k2則不能才產(chǎn)生新匹配,跳過(guò)若存在這樣的k1,k2,此時(shí)匹配(a[i],b[k2])、(a[k1],b[j])匹配,才生新的匹配情形,匹配數(shù)量為:dp[k1-1][k2-1]+2。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=100+10;int n,m;int a[maxn],b[maxn];int d[maxn][maxn];int main(){    int cas;    scanf("%d",&cas);    while(cas--)    {        scanf("%d%d",&n,&m);        rep(i,1,n+1) scanf("%d",&a[i]);        rep(i,1,m+1) scanf("%d",&b[i]);        memset(d,0,sizeof(d));        rep(i,2,n+1) rep(j,2,m+1)        {            d[i][j]=max(d[i][j-1],d[i-1][j]);            if(a[i]==b[j]) continue;            else            {                int p1=0,p2=0;                for(int k=i-1;k>0;k--)                {                    if(a[k]==b[j])                    {                        p1=k;                        break;                    }                }                for(int l=j-1;l>0;l--)                {                    if(b[l]==a[i])                    {                        p2=l;                        break;                    }                }                if(p1&&p2) d[i][j]=max(d[i][j],d[p1-1][p2-1]+2);            }        }        printf("%d/n",d[n][m]);    }        return 0;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费黄色在线观看网站 | 国产羞羞视频在线免费观看 | 久久免费精品视频 | 国产网站黄 | 91香蕉国产亚洲一区二区三区 | 视频在线中文字幕 | 深夜免费观看视频 | 亚洲国产视频网 | 久久久久久久久久网 | 亚洲日本高清 | 成人国产视频在线观看 | 国产成人精品网站 | japanese xxxxhd| 4399一级成人毛片 | 日韩中文字幕一区二区三区 | 欧日韩| 一级国产精品一级国产精品片 | 欧美伦交| 国产一级伦理片 | 毛片毛片免费看 | 欧美一级三级在线观看 | 欧美日本亚洲视频 | 国产porn在线 | 毛片免费视频播放 | 国产色爱综合网 | 亚洲va久久久噜噜噜久久男同 | 电影av在线 | 美女在线观看视频一区二区 | 久久精品高清 | 日韩毛片一区二区三区 | 欧美在线观看视频一区 | 国产精品99精品 | av免费片 | 国产成人综合在线 | www.777含羞草 | 国产国语毛片 | 永久免费av在线 | 欧美a视频 | 免费看真人a一级毛片 | 久久久精品视频国产 | 欧美性色黄大片www 操碰网 |