zoukankan      html  css  js  c++  java
  • Binary Search Aizu

    Search II

    You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.

    Input

    In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.

    Output

    Print C in a line.

    Constraints

    Elements in S is sorted in ascending order
    n ≤ 100000
    q ≤ 50000
    0 ≤ an element in S ≤ 109
    0 ≤ an element in T ≤ 109

    Sample Input 1

    5
    1 2 3 4 5
    3
    3 4 1

    Sample Output 1

    3

    Sample Input 2

    3
    1 2 3
    1
    5

    Sample Output 2

    0

    Sample Input 3

    5
    1 1 2 2 3
    2
    1 2

    Sample Output 3

    2

    code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00             ^   11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                  00.                                     ^^0.^
                                  ^ 0                                     ^^110.^
                              0   0 ^                                     ^^^10.01
                       ^^     10  1 1                                      ^^^1110.1
                       01     10  1.1                                      ^^^1111110
                       010    01  ^^                                        ^^^1111^1.^           ^^^
                       10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    // Virtual_Judge —— Binary Search Aizu - ALDS1_4_B.cpp created by VB_KoKing on 2019-05-01:18.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first gentle breeze that passed through my ear is you,
    The first star I see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <iostream>
    using namespace std;
    int A[1000007],n;
    
    //二分搜索
    int binary_search(int key)
    {
        int left=0,right=n,mid;
        while (left<right)
        {
            mid=(left+right)/2;
            if (key==A[mid]) return 1;
            if (key>A[mid]) left=mid+1;
            else if (key<A[mid]) right=mid;
        }
        return 0;
    }
    
    int main()
    {
        int q,key,sum=0;
        cin>>n;
        for (int i = 0; i < n; i++)
            cin>>A[i];
        cin>>q;
        for (int i = 0; i < q; i++) {
            cin>>key;
            if (binary_search(key))
                sum++;
        }
    cout<<sum<<endl;
        return 0;
    }
    
  • 相关阅读:
    为什么要前后端分离?有什么优缺点
    剑指offer-面试题21.包含min函数的栈
    操作系统典型调度算法
    那些年的那些事CISC和RISC发展中的纠缠
    基于MFC与第三方类CWebPage的百度地图API开发范例
    Linux进程通信----匿名管道
    续前篇-关于逆波兰表达式的计算
    逆波兰表达式的实现(也叫后缀表达式)
    剑指offer-面试题20.顺时针打印矩阵
    剑指offer-面试题.二叉树的镜像
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338374.html
Copyright © 2011-2022 走看看