Time Limit: 2 second(s) | Memory Limit: 32 MB |
As you probably know, the human information processor is a wonderful text recognizer that can handle even sentences that are garbled like the following:
The ACM Itrenntaoial Clloegaite Porgarmmnig Cnotset (IPCC) porvdies clolgee stuetnds wtih ooppriuntetiis to itnrecat wtih sutednts form ohetr uinevsrtieis.
People have claimed that understanding these sentences works in general when using the following rule: The first and last letters of each word remain unmodified and all the characters in the middle can be reordered freely. Since you are an ACM programmer, you immediately set on to write the following program: Given a sentence and a dictionary of words, how many different sentences can you find that could potentially be mapped to the same encoding?
Input
Input starts with an integer T (≤ 20), denoting the number of test cases.
Each case starts with a line containing the number n (0 ≤ n ≤ 10000) of words in the dictionary, which are printed on the following n lines. After this, there is a line containing the number m (0 ≤ m ≤ 10000)of sentences that should be tested with the preceding dictionary and then m lines containing those sentences. The sentences consist of letters from a to z, A to Z and spaces only and have a maximal length of10000 characters. For each word in the dictionary a limitation of 100 characters can be assumed. The words are case sensitive. In any case, total number of characters in the sentences will be at most 105. And total characters in the dictionary will be at most 105.
Output
For each case, print the case number first. Then for each sentence, output the number of sentences that can be formed on an individual line. Result fits into 32 bit signed integer.
Sample Input |
Output for Sample Input |
1 8 baggers beggars in the blowed bowled barn bran 1 beggars bowled in the barn |
Case 1: 8 |
Note
Dataset is huge, use faster I/O methods.
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<queue> 6 #include<stack> 7 #include<map> 8 #include<math.h> 9 #include<stack> 10 using namespace std; 11 typedef long long LL; 12 char str[105]; 13 char bb[100006]; 14 char ak[105]; 15 void in(char *v); 16 int ask(char *vv); 17 int tree[100006][52]; 18 int val[100006]; 19 int nn=0; 20 int sk=1; 21 int main(void) 22 { 23 int i,j,k; 24 scanf("%d",&k); 25 int s; 26 for(s=1; s<=k; s++) 27 { 28 int n,m;sk=1; 29 memset(tree,-1,sizeof(tree)); 30 memset(val,0,sizeof(val)); 31 scanf("%d ",&n); 32 for(i=0; i<n; i++) 33 { 34 scanf("%s",str); 35 int l=strlen(str); 36 if(l>=3) 37 sort(str+1,str+l-1); 38 str[l]='