Question: There are N products with names consisting of lower-case letters and M inquiries. Each inquiry comes with a K and then a K bar code. Each bar code represents one lower-case letter, these K bar codes are connected into consecutive K letters as the prefix. The number of product name prefixes is counted (repeated ), output the statistics of M queries (1 <= N <= 10000, 1 <= M <= 2000, 0 <K <= 30, Product Name Length <= 30 ).
--> After the Trip is organized, convert the bar code to lowercase letters (1 if the value is greater than the average value, or 0 if the value is greater than the average value, and calculate the ASCII code), connect the strings, locate and sum the values in the Trip.
#include <cstdio>#include <cstring>using namespace std;const int maxn = 30 + 5;const int maxb = 8 + 5;int N, M, K, bit[maxb];double bar[maxb];char qus[maxn];struct node{ int cnt; node *next[26]; node(){ cnt = 0; memset(next, 0, sizeof(next)); }};struct Trip{ node *root; int ret; Trip(){ root = new node; ret = 0; } int idx(char c){ return c - 'a'; } void insert(char *s){ int len = strlen(s), i; node *t = root; for(i = 0; i < len; i++){ int c = idx(s[i]); if(!t->next[c]) t->next[c] = new node; t->next[c]->cnt++; t = t->next[c]; } } void solve(){ node *t = root; int len = strlen(qus), i; for(i = 0; i < len; i++){ int c = idx(qus[i]); if(!t->next[c]) break; t = t->next[c]; } if(i == len) ret += t->cnt; } void print(){ printf("%d\n", ret); }};int main(){ char product[maxn]; int i, j, k; while(scanf("%d%d", &N, &M) == 2){ Trip trip; for(i = 0; i < N; i++) { scanf("%s", product); trip.insert(product); } for(i = 0; i < M; i++){ int cnt = 0; scanf("%d", &K); for(j = 0; j < K; j++){ double sum = 0; for(k = 0; k < 8; k++){ scanf("%lf", &bar[k]); sum += bar[k]; } sum /= 8; int ASCII = 0; for(k = 0; k < 8; k++) if(bar[k] > sum) ASCII += (1 << (7-k)); qus[cnt++] = (char)ASCII; } qus[cnt] = '\0'; trip.solve(); } trip.print(); } return 0;}