比較兩個已排序的序列其共同的部分有多少即可得解。
C++(0.470)
/*******************************************************/
/* UVa 11849 CD */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2018/05/14 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main(){
int N, M;
while (scanf("%d%d", &N, &M) != EOF &&
N != 0 && M != 0){
vector<int> nCDs(N, 0);
vector<int> mCDs(M, 0);
for (int i = 0 ; i < N ; ++i ){
scanf("%d", &nCDs[i]);
}
for (int i = 0 ; i < M ; ++i ){
scanf("%d", &mCDs[i]);
}
int bothOwnCount = 0;
for (int i = 0, j = 0 ; i < N && j < M ; ){
if(nCDs[i] == mCDs[j]){
++bothOwnCount;
++i;
++j;
}
else if(nCDs[i] > mCDs[j]) ++j;
else ++i;
}
printf("%d\n", bothOwnCount);
}
return 0;
}