可能开始宿舍比较乱,思绪静不下来。。。想了大半个小时,终于确定了应该暴力+DP,然后写了枚举除数,和被除的版本。。这样,还敲错了个字母,第一次提交还100多,修改提交还有75.多,最后想到,貌似不打对啊,改完再交就剩下75了。。。还好,没挂0。。。这样写,还是比较好写的,最后10分钟,开始改,最后4分钟改完。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define LL long long
int dp[][];
int gcd(int a,int b)
{
return b == ?a:gcd(b,a%b);
}
LL s1[];
LL s2[];
int dfs(int n,int m)
{
int i,j;
memset(dp,,sizeof(dp));
for(i = ; i <= n; i ++)
{
for(j = ; j <= m; j ++)
{
if(s1[i-] == s2[j-])
dp[i][j] = dp[i-][j-] + ;
else
dp[i][j] = max(dp[i-][j],dp[i][j-]);
}
}
return dp[n][m];
}
class AstronomicalRecords
{
public :
int minimalPlanets(vector <int> A, vector <int> B)
{
int i,j,k,maxz;
for(i = ; i < A.size(); i ++)
s1[i] = A[i];
for(i = ; i < B.size(); i ++)
s2[i] = B[i];
maxz = dfs(A.size(),B.size()); for(i = ; i < A.size(); i ++)
{ for(j = ; j < B.size(); j ++)
{
for(k = ; k < A.size(); k ++)
s1[k] = (LL)B[j]*A[k];
for(k = ; k < B.size(); k ++)
s2[k] = (LL)A[i]*B[k];
maxz = max(maxz,dfs(A.size(),B.size()));
} }
return A.size() + B.size() - maxz;
}
};