Encoded barcodes
Time limit:3000 Ms
Memory limit:0 KB
64bit Io format:% LLD & % llusubmit status practice uvalive 5029
Description
All the big mallneed a powerful system for the products retrieval. Now you are employed design a sub-system: Reading the barcodes and return the matching products.
A barcode is an optical machine-readable representation of data, which shows certain data on certain products. A barcode consists of a series of bars with different widths. in our system, the barcodes have been scanned and The widths have been recorded. every consecutive eight bars are considered as representing the ASCII code of a character, each bar for each bit. ideally, there shoshould be only two kinds of widths in the eight bars, and the width of the wider bar is twice of the narrower. the wider bar indicates 1, while the narrower indicates 0. however, due to the inaccuracy of printing and scanning, there will be an error of at most 5%. that is, if the pretended exact width isX, You may get a value in the range [0.95X, 1.05X].
For example, the width sequence ''10. 0 20.0 10.0 10.0 10.0 10.0 10.0 20.0 "is a valid barcode of our system, and it means (01000001) 2, which is (65) 10 and the corresponding character is ''a ". note that ''10. 5 20.1 10.1 10.2 9.9 9.7 10.0 19.9 "is also a valid barcode representing the same letter.
You are given the names of all the products and your queries. every name contains lower-case letters only, and the length is no more than 30. the queries are represented as barcodes. for each query, you shoshould decode it to a stringS, And report the amount of products whose prefix isS. For the output may be very large, you only need to output the sum of all the queries for each case.
Input
There are several test cases in the input. The first line of each case contains two integersNAndM(1N10000, 1M2000), indicating the number of products and queries. ThenNLines follow, indicating the names of the products. Note that the names may be duplicated. ThenMQuery blocks follow. The first line of each query block is an integerK(0 <K30) indicating the length of the query, thenKLines follow, each line contains 8 positive float numbers, indicating the barcode for each character.
You can assume that the barcodes are always valid, and always represent lower-case letters.
Output
Output one line for each test case, indicating the sum of all the query results as described above.
Explanation for the sample:
There is only one test case. The first query is''A", And the answer is 3. The second query is''AP", And the answer is 2. The third query is''C", And the answer is 0. So the total sum is 3 + 2 + 0 = 5.
Sample Input
4 3 appleapple avatar book 1 1 2 2 1 1 1 1 2 2 1 2 2 1 1 1 1 210.1 20.1 19.9 20.0 10.2 9.8 9.9 10.01 1 2 2 1 1 1 2 2
Sample output
5
Question and code:
/* The capabilities are limited. They can only be written using brute force. We can see that all of them are written using dictionary trees. The time is about 100 + MS, but the violent time is 800 + MS, the gap is not small. If you have time, you need to make up for it. */# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # define maxn 11111 using namespace STD; typedef long ll; int main () {char s [1, 10010] [35]; double d [9]; memset (S, 0, sizeof (s); int n, m; while (scanf ("% d", & N, & M )! = EOF) {int ans = 0; For (INT I = 0; I <n; I ++) {scanf ("% s", s [I]);} int K; For (INT I = 0; I <m; I ++) {scanf ("% d", & K); char T [35]; memset (t, 0, sizeof (t); For (Int J = 0; j <K; j ++) {double MI = 0; For (int v = 0; v <= 7; V ++) {scanf ("% lf", & D [v]); MI + = d [v];} MI/= 8.0; // obtain the average value of 8 numbers. The value above the average value is a wide bar code, that is, 1 // The value below the average value is a narrow bar code, that is, 0 int B = 1, sum = 0; For (INT v = 7; V> = 0; V --) {If (d [v]> mi) sum + = B; B * = 2 ;} T [J] = sum;} // cout <t <Endl; For (int v = 0; v <n; V ++) if (strstr (s [v], t) = s [v]) // The strstr function returns the matched position of the substring in the parent string. {// null is returned if the substring is missing. If it is equal to s [v], it indicates the prefix is ans ++ ;}} printf ("% d \ n", ANS);} return 0 ;}