标签:
| input | output |
|---|---|
2 1054 1492 4 1492 65536 1492 100 |
2 |
题意:找出第一和第二个序列中都出现(允许重复累加)过的字符个数。
解析:由于第一个有序的,所以我们遍历第二个序列的同时对第一个序列二分搜索答案。
PS:本题有个很诡异的现象:G++跑了1.5s+,但是VC++竟然才跑0.343s。。。貌似只有VC++才能过。
AC代码:
#include <cstdio>
using namespace std;
int a[15002];
int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk
int n, m, ans, foo;
while(scanf("%d", &n)==1){
ans = 0;
for(int i=0; i<n; i++) scanf("%d", &a[i]);
scanf("%d", &m);
for(int i=0; i<m; i++){
scanf("%d", &foo);
int l = 0, r = n - 1, m;
if(foo < a[0] || foo > a[n-1]) continue;
else if(foo == a[0] || foo == a[n-1]){
ans ++;
continue;
}
while(l <= r){
m = (r - l) / 2 + l;
if(a[m] == foo){
ans ++;
break;
}
if(a[m] < foo) l = m + 1;
else r = m - 1;
}
}
printf("%d\n", ans);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/u013446688/article/details/44224707