1233: MCC同学拒绝重复代码
时间限制: 1 Sec 内存限制: 16 MB提交: 56 解决: 13
题目描述
作为程序员,在工程中需要掌握的技能之一,就是避免代码的重复。 MCC同学开发了一套自动检测重复代码的软件,现在就剩下算法部分未完成了。 我们要求给出两个文件的内容,通过计算其中“最长公共子序列”的长度,来计算其代码重复度。 通过MCC牌黑科技,我们将文件的内容变成了一个整数数组,来便于进行比较。
输入
第一行两个整数n和m 第二行,第三行分别有n,m个整数,表示文件内容。 保证n,m<6000,保证文件内容数字<10000
输出
输出一行整数,表示最长公共子序列的长度。
样例输入
5 71 0 2 0 2 2 1 1 1 2 0 0
样例输出
3
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
const double PI = acos(-1.0);
///
///
int main(int argc, char**argv) {
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("input.txt", "r", stdin);?
//freopen("output.txt", "w", stdout);
int n,m;
while(cin>>n>>m){
int st[2][8000];
memset(st,0,sizeof(st));
int nl[8000];
int ml[8000];
for(int i=1;i<=n;i++){
cin>>nl[i];
}
for(int i=1;i<=m;i++){
cin>>ml[i];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(nl[i]==ml[j]){
st[i&1][j]=st[(i-1)&1][j-1]+1;
}
else{
st[i&1][j]=max(st[i&1][j-1],st[(i-1)&1][j]);
}
}
}
cout<<st[n&1][m]<<endl;
}
//system("pause");
return 0;
}
//END
/**************************************************************
Problem: 1233
User: 2015217298
Language: C++
Result: 正确
Time:389 ms
Memory:1544 kb
****************************************************************/