String search (Binary Search and quick search)

Source: Internet
Author: User

Description Now let's give you a dictionary, and then give several strings for you to find out whether these strings are in it.


 

Input The first line is two integers, M, and N, respectively, indicates the number of dictionaries and strings.
From row 2nd to row m + 1, each row is a dictionary.
Lines m + 2 to m + 2 + N are the strings searched by Xu.
(N <= 10,0000, m <= 20,0000, the string length cannot exceed 10, and all are lowercase letters)


 

Output N rows in total. Each row indicates that the I-th string is not in the dictionary, 0 indicates that it is not in, and 1 indicates that it is in.


 

Sample Input 5 3
ACC
Cat
Multiply
Do
Will
Accept
Cat
A


 

Sample output 0
1
0

 

Solution:


When I got this question, the first idea was to compare each word to be searched with the words in the dictionary one by one. However, when I read the data, n <=, m <=, the data was so big, if one-to-one comparison will definitely time out, then we will look at the number of AC users. It seems that the question is not as easy as we expected. We have to use some previously learned algorithms to solve the problem. In retrospect, binary is the most well-known one that can improve the search efficiency. Therefore, I plan to use binary to answer this question. To use binary classification, you must sort the words in the dictionary in Lexicographic Order. The efficiency is nothing more than the fast sorting function. Now, the entire question is easily solved. It is just a basic question that is sorted first and then second. However, when I submitted it with G ++, it turned out to be re. Then I switched to C ++ and submitted it to the AC. I don't understand this. Which of the following experts can answer this question for me.

 

AC code:

 

# Include <iostream> # include <cstdio> # include <algorithm> # include <string> # define max_num 100005 using namespace STD; string dic_1 [max_num * 2], word_1 [max_num * 2]; bool CMP (string a, string B) // defines a comparison function so that it is sorted alphabetically {if (. compare (B) <= 0) return true; else return false;} int main () {int M, N; char dic_2 [15], word_2 [15]; scanf ("% d", & M, & N); For (INT I = 0; I <m; I ++) {scanf ("% s ", dic_2); dic_1 [I] = d Ic_2; // The input efficiency of scanf is higher than that of CIN. Therefore, I use scanf to input a string and then assign it to string} For (INT I = 0; I <n; I ++) {scanf ("% s", word_2); word_1 [I] = word_2;} Sort (dic_1, dic_1 + M, CMP ); // sort for (INT I = 0; I <n; I ++) {int low = 0, high = M-1, mid; bool isfind = false; while (low <= high) // Binary Search {mid = (low + high)/2; If (word_1 [I]. compare (dic_1 [Mid]) = 0) {printf ("1 \ n"); // if it is found, 1 isfind = true; // confirm that the break has been found ;} if (wor D_1 [I]. compare (dic_1 [Mid])> 0) Low = Mid + 1; if (word_1 [I]. compare (dic_1 [Mid]) <0) High = mid-1 ;}if (! Isfind) printf ("0 \ n"); // output 0 cannot be found} 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.