Sicily -- simple binary search and sorting

Source: Internet
Author: User

Recently I wrote a question about string search. The question is: give you a sequence of strings and then give another sequence of strings, find the number of strings in the first sequence that have not appeared in the second sequence, ignore the case.

For example:

First sequence:

Inkfish

Henry

Carp

Max

Jericho

The second sequence:

Carp

Max

Carp

 

Result: 3

 

First, convert the first sequence to lowercase, and then sort the first sequence. 2) use binary search to search for the second sequence to see if it has appeared in the first sequence:

# Include "stdafx. H"
# Include <iostream>
# Include <algorithm>
# Include <cstring>
# Include <string>
Using namespace STD;
String S1 [20000];
String S2 [20000];
Int flag [20001];
Int bfind (string S, int low, int high) {// binary lookup Function
Int L, h;
L = low;
H = high;
Int mid = (L + H)/2;
If (L> H)
Return-1;
If (S = S1 [Mid])
Return mid;
Else if (S> S1 [Mid])
Return bfind (S, Mid + 1, high );
Else
Return bfind (S, low, mid-1 );
 
}
Int main (){
Int n, m;
While (CIN> N & n! = 0 ){
Cin> m;
Int COUNT = 0;
Memset (flag, 0, sizeof (FLAG ));
For (INT I = 0; I <n; I ++ ){
Cin> S1 [I];
For (Int J = 0; j <S1 [I]. Length (); j ++) {// converts all characters to lowercase letters.
If (S1 [I] [J] <97)
S1 [I] [J] = S1 [I] [J] + 32;
}
}
Sort (S1, S1 + n );
For (INT I = 0; I <m; I ++ ){
Cin> S2 [I];
For (Int J = 0; j <S2 [I]. Length (); j ++) {// converts all characters to lowercase letters.
If (s2 [I] [J] <97)
S2 [I] [J] = S2 [I] [J] + 32;
}
}
For (Int J = 0; j <m; j ++ ){
Int A = bfind (s2 [J], 0, n-1 );
If (! =-1 & flag [a] = 0 ){
Count ++;
Flag [a] = 1;
}
}
Cout <(n-count) <Endl;
Count = 0;
}
Return 0;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.