直接贪心做法,匹配最小的
#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=123,MOD=1e9+7;
int boys[111];
int girls[111];
int main()
{
int n,m;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&boys[i]);
scanf("%d",&m);
for(int i=0;i<m;i++)
scanf("%d",&girls[i]);
sort(boys,boys+n);
sort(girls,girls+m);
int ans = 0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(abs(boys[i] - girls[j] )<=1){
girls[j] = 1111;
//cout<<i <<" "<<j<<endl;
ans++;
break;
}
}
}
printf("%d\n",ans);
return 0;
}
最长公共子序列LCS
#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=123,MOD=1e9+7;
int boys[111];
int girls[111];
int dp[111][111];
int main()
{
int n,m;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&boys[i]);
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d",&girls[i]);
sort(boys+1,boys+n+1);
sort(girls+1,girls+m+1);
int ans = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j] = max(dp[i][j],dp[i][j-1]);
dp[i][j] = max(dp[i][j],dp[i-1][j]);
if(abs(boys[i] - girls[j] )<=1){
dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
}
}
}
printf("%d\n",dp[n][m]);
return 0;
}