~~~~
Question: Give a sequence of two numbers, and find the maximum number of upper and lower matching groups.
Matching rules:
1. The matching numbers must be the same.
2. Each matching must have only one matching that is matched with each other, and the matching numbers must be different.
3. A single number can only be matched once.
For the adaptation of the longest common subsequence, F [I] [J] indicates the optimal value of matching between the first I digit of the first sequence and the first J digit of the second sequence.
State Transfer: F [I] [J] = max (F [P-1] [q-1] + 2, max (F [I-1] [J], f [I] [J-1])
& (A [p] = B [J], B [Q] = A [I]);
* Updating f [I] [J] with f [P-1] [q-1] ensures that a single number can only be matched once.
Question link: http://poj.org/problem? Id = 1692
~~~~
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#define N 1000using namespace std;int f[N][N];int a[N],b[N];int main(){ int t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); int i,j,p,q; for(i=1;i<=n;i++) scanf("%d",&a[i]); for(j=1;j<=m;j++) scanf("%d",&b[j]); memset(f,0,sizeof(f)); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { for(q=j-1;q>0;q--) if(b[q]==a[i]) break; for(p=i-1;p>0;p--) if(a[p]==b[j]) break; if(p>0 && q>0 && a[i]!=b[j]) //~~~ f[i][j]=max(f[p-1][q-1]+2,max(f[i][j-1],f[i-1][j])); else f[i][j]=max(f[i][j-1],f[i-1][j]); } } printf("%d\n",f[n][m]); } return 0;}