Head-chef Monica
Practice
4.6 (9 votes)
Hash function
Ad Hoc
Implementation
Hash table
Open
Approved
Easy
Problem
89% Success 4242 Attempts 20 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Monica is the Head-chef at Javu's restaurant. Javu has N kitchen workers, numbered from 1 to N. Each kitchen worker has expertise in exactly one of the following fields:

  1. VEG - denoted as V
  2. NONVEG - denoted as N
  3. GRILL - denoted as G
  4. TOAST - denoted as T
  5. SANDWICH - denoted as S
  6. BURGER - denoted as B
  7. PIZZA - denoted as P

You'll be given the string S1 of length N, where ith character defines the field of expertise of ith worker, 1 ≤ i ≤ N.
Now, a customer's order contains 3 fields:

1st field 2nd field 3rd field
VEG or NONVEG (V or N) GRILL or TOAST (G or T) SANDWICH or BURGER or PIZZA (S or B or P)

You'll be given a string S2 of length 3, where jth character defines the jth field of the order, 1 ≤ j ≤ 3.
The first character will either be V or N. Second character will either be G or T. Third character will either be S or B or P.

According to the customer order, Monica has to form a team of workers who can prepare the order. Javu has received M orders at the moment. You have to find in how many ways can Monica prepare a team of 3 workers for each of the orders.

Note: One worker can only work in one team. See the sample test case for clarification.

Input:
First line of input contains N - number of workers.
Second line contains a string S1 of length N, where ith character defines the field of expertise of ith worker, 1 ≤ i ≤ N.
Third lines contains M - number of orders.
Each of the next M lines contain an order query S2.

Output:
For each order query, print the number of ways Monica can form a team of 3 workers who can prepare the order.
Since the answer can be very large, print it modulo 1000000007 ( 109 + 7 )

Constraints:
1 ≤ N ≤ 1000000
String S1 will be of length N and will only contain upper case character [V, N, G, T, S, B, P]
1 ≤ M ≤ 300000
String S2 will be of length 3 and will only contain upper case character [V, N, G, T, S, B, P]

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
9 votes
Tags:
OpenApprovedHashingEasy
Points:20
12 votes
Tags:
Hash tableHashMapBasic ProgrammingEasyOpenApproved
Points:20
8 votes
Tags:
Hash functionImplementationBinary search treeData StructuresHash tableOpenApprovedEasy