1114 - Easily Readable

   PDF (English) Statistics Forum
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 zA 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.



SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)

字典树;要用静态数组否则MLE;

  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]=‘\0‘;
 39             in(str);
 40         }
 41         scanf("%d",&m);
 42         printf("Case %d:\n",s);
 43         getchar();
 44         while(m--)
 45         {
 46             gets(bb);
 47             if(bb[0]==‘\0‘)printf("1\n");
 48             else
 49             {
 50                 int l=strlen(bb);
 51                 int uu=0;
 52                 int flag=0;
 53                 LL sum=1;
 54                 bb[l]=‘ ‘;
 55                 for(i=0; i<=l; i++)
 56                 {
 57                     if(bb[i]!=‘ ‘)
 58                     {
 59                         flag=1;
 60                         ak[uu++]=bb[i];
 61                     }
 62                     else if(flag==1&&bb[i]==‘ ‘)
 63                     {
 64                         flag=0;
 65                         ak[uu]=‘\0‘;
 66                         if(uu>=3)
 67                             sort(ak+1,ak+uu-1);
 68                         sum*=(LL)ask(ak);
 69                         uu=0;
 70                     }
 71                 }
 72                 printf("%lld\n",sum);
 73             }
 74         }
 75     }
 76     return 0;
 77 }
 78 void in(char *v)
 79 {
 80     int l=strlen(v);
 81     int i,j;
 82     int cc;
 83     int k=0;
 84     for(i=0; i<l; i++)
 85     {
 86         if(v[i]>=‘A‘&&v[i]<=‘Z‘)
 87         {
 88             cc=v[i]-‘A‘+26;
 89         }
 90         else
 91             cc=v[i]-‘a‘;
 92         if(tree[k][cc]==-1)
 93         {
 94             tree[k][cc]=sk;
 95             k=sk;
 96             sk++;
 97             nn++;
 98         }
 99         else k=tree[k][cc];
100     }
101     val[k]++;
102 }
103 int ask(char *vv)
104 {
105     int l=strlen(vv);
106     int i,j;
107     int cc;
108     int ak=0;
109     for(i=0; i<l; i++)
110     {
111         if(vv[i]>=‘A‘&&vv[i]<=‘Z‘)
112         {
113             cc=vv[i]-‘A‘+26;
114         }
115         else
116             cc=vv[i]-‘a‘;
117         if(tree[ak][cc]==-1)
118             return 0;
119         ak=tree[ak][cc];
120     }
121     return val[ak];
122 }
时间: 2024-08-03 23:47:21

1114 - Easily Readable的相关文章

Light OJ 1114 Easily Readable 字典树

题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 只要满足每个单词的首尾字符一样 中间顺序可以变化 思路:每个单词除了首尾 中间的字符排序 然后插入字典树 记录每个单词的数量 输入一个句子 每个单词也排序之后查找 根据乘法原理 答案就是每个单词的数量之积 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm>

MySQL入门手册

本文内容摘自MySQL5.6官方文档,主要选取了在实践过程中所用到的部分文字解释,力求只摘录重点,快速学会使用MySQL,本文所贴代码地方就是我亲自练习过的代码,凡本文没有练习过的代码都没有贴在此处,如果读者想自己尝试,可以查看官方文档,文中给出了原官方文档的对应链接以供查阅. 本文地址:http://www.cnblogs.com/yhLinux/p/4019386.html http://dev.mysql.com/doc/refman/5.6/en/tutorial.htmlThis ch

官方的objective - c风格指南。

The official raywenderlich.com Objective-C style guide. This style guide outlines the coding conventions for raywenderlich.com. Introduction The reason we made this style guide was so that we could keep the code in our books, tutorials, and starter k

About SQLite

About SQLite See Also... Features When to use SQLite Frequently Asked Questions Well-known Users Books About SQLite Getting Started SQL Syntax Pragmas SQL functions Date & time functions Aggregate functions C/C++ Interface Spec Introduction List of C

Objective-C编码规范[译]

原文链接 : The official raywenderlich.com Objective-C style guide 原文作者 : raywenderlich.com Team 译文出自 : raywenderlich.com Objective-C编码规范 译者 : Sam Lau 因为我正在准备模仿饿了么这个app,到时可能有些iOS开发人员參与进来. 这时假设每一个人的Objective-C编码风格都不一样,这样不易于保持代码一致性和难以Code Review.所以我在网上搜索到 T

使用神经网络来识别手写数字【译(三)- 用Python代码实现

实现我们分类数字的网络 好,让我们使用随机梯度下降和 MNIST训练数据来写一个程序来学习怎样失败手写数字. 我们也难怪Python (2.7) 来实现.只有 74 行代码!我们需要的第一个东西是 MNIST数据.如果有 github 账号,你可以将这些代码库克隆下来, git clone https://github.com/mnielsen/neural-networks-and-deep-learning.git 或者你可以到这里 下载. Incidentally, 当我先前说到 MNIS

Boost 1.61.0 Library Documentation

http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for incremental calculation, and collection of statistical accumulators. Author(s): Eric Niebler First Release: 1.36.0 Standard: Categories: Math and nume

raywenderlich.com Objective-C编码规范

原文链接 : The official raywenderlich.com Objective-C style guide 原文作者 : raywenderlich.com Team 译文出自 : raywenderlich.com Objective-C编码规范 译者 : Sam Lau 由于我正在准备模仿饿了么这个app,到时可能有些iOS开发者参与进来.这时如果每个人的Objective-C编码风格都不一样,这样不易于保持代码一致性和难以Code Review.所以我在网上搜索到 The

Linux Perf Probes for Oracle Tracing

Luca Canali on 21 Jan 2016 Topic: this post is about Linux perf and uprobes for tracing and profiling Oracle workloads for advanced troubleshooting. Context The recent progress and maturity of some of the Linux dynamic tracing tools has raised intere