24h購物| | PChome| 登入
2012-05-10 18:04:20| 人氣1,668| 回應0 | 上一篇 | 下一篇

[UVA] 11849 - CD

推薦 0 收藏 0 轉貼0 訂閱站台

Problem A: CD

Jack and Jill have decided to sell some of their Compact Discs, while they still have some value. They have decided to sell one of each of the CD titles that they both own. How many CDs can Jack and Jill sell?

Neither Jack nor Jill owns more than one copy of each CD.

Input Specification

The input consists of a sequence of test cases. The first line of each test case contains two non-negative integers N and M, each at most one million, specifying the number of CDs owned by Jack and by Jill, respectively. This line is followed by N lines listing the catalog numbers of the CDs owned by Jack in increasing order, and M more lines listing the catalog numbers of the CDs owned by Jill in increasing order. Each catalog number is a positive integer no greater than one billion. The input is terminated by a line containing two zeros. This last line is not a test case and should not be processed.

Sample Input

3 3
1
2
3
1
2
4
0 0

Output Specification

For each test case, output a line containing one integer, the number of CDs that Jack and Jill both own.

Output for Sample Input

2



// C++ Rank 5
#include <stdio.h>
int A[1000000];
inline int readchar() {
const int N = 1048576;
static char buf[N];
static char *p = buf, *end = buf;
if(p == end) {
if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF;
p = buf;
}
return *p++;
}
inline int ReadInt(int *x) {
static char c, neg;
while((c = readchar()) < '-') {if(c == EOF) return 0;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = readchar()) >= '0')
*x = (*x << 3) + (*x << 1) + c-'0';
*x *= neg;
return 1;
}
int main() {
int n, m;
while(ReadInt(&n)) {
ReadInt(&m);
if(n == 0 && m == 0)
break;
int idx = 0, i, x, cnt = 0;
for(i = 0; i < n; i++) {
ReadInt(A+i);
}
while(m--) {
ReadInt(&x);
while(idx < n) {
if(A[idx] == x)
cnt++, idx++;
else if(A[idx] < x)
idx++;
else
break;
}
}
printf("%d\n", cnt);
}
return 0;
}

台長: Morris
人氣(1,668) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][字串處理] 444 - Encoder and Decoder
此分類上一篇:[UVA][大數] 324 - Factorial Frequencies

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文