Description

Before the digital age, the most common "binary" code for radio communication was the Morse code. In Morse code, symbols are encoded as sequences of short and long pulses (called dots and dashes respectively). The following table reproduces the Morse code for the alphabet, where dots and dashes are represented as ASCII characters "." and "-":

Notice that in the absence of pauses between letters there might be multiple interpretations of a Morse sequence. For example, the sequence -.-..-- could be decoded both as CAT or NXT (among others). A human Morse operator would use other context information (such as a language dictionary) to decide the appropriate decoding. But even provided with such dictionary one can obtain multiple phrases from a single Morse sequence. 

Task 

Write a program which for each data set: 

reads a Morse sequence and a list of words (a dictionary), 

computes the number of distinct phrases that can be obtained from the given Morse sequence using words from the dictionary, 

writes the result. 

Notice that we are interested in full matches, i.e. the complete Morse sequence must be matched to words in the dictionary.

Input

The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 20. The data sets follow. 

The first line of each data set contains a Morse sequence - a nonempty sequence of at most 10 000 characters "." and "-" with no spaces in between. 

The second line contains exactly one integer n, 1 <= n <= 10 000, equal to the number of words in a dictionary. Each of the following n lines contains one dictionary word - a nonempty sequence of at most 20 capital letters from "A" to "Z". No word occurs in the dictionary more than once.

Output

The output should consist of exactly d lines, one line for each data set. Line i should contain one integer equal to the number of distinct phrases into which the Morse sequence from the i-th data set can be parsed. You may assume that this number is at most 2 * 10^9 for every single data set.

Sample Input

1
.---.--.-.-.-.---...-.---.
6
AT
TACK
TICK
ATTACK
DAWN
DUSK

Sample Output

2

解题思路:

字典配对问题。就是把输入的各个字符串先转化为"..--"等形式,然后对于给定的字符串,如题中的".---.--.-.-.-.---...-.---.",从第0位开始和下面的候选字符串开始配对。采用dp的思想,dp[i]表示前i个已经配对的情况数。如果dp[i]不为0,表示当前的情况可以由之前的情况转化。

# include<stdio.h>
# include<string.h>
# define N 10050 char cod[26][10] = {{".-"},{"-..."},{"-.-."},{"-.."},{"."},{"..-."},{"--."},
{"...."},{".."},{".---"},{"-.-"},{".-.."},{"--"},{"-."},{"---"},{".--."},
{"--.-"},{".-."},{"..."},{"-"},{"..-"},{"...-"},{".--"},{"-..-"},{"-.--"},{"--.."}}; char final[N][N];//用来存储转化后的字符串
int dp[N];
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
// printf("t %d",t);
while(t--)
{
char str[N];
scanf("%s",str);
int Lens=strlen(str);
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
{
char tmp[N];
scanf("%s",tmp);
//final[i]需要清零
final[i][0]='\0';
int len=strlen(tmp);
for(j=0;j<len;j++)
{
strcat(final[i],cod[tmp[j]-'A']);
}
}
memset(dp,0,sizeof(dp));
dp[0]=1; for(i=0;i<Lens;i++)
{
if(dp[i]!=0)
{
for(j=0;j<n;j++)
{
int tmpL=strlen(final[j]);
if(strncmp(str+i,final[j],tmpL)==0)//很巧妙
dp[i+tmpL]+=dp[i];
}
}
} printf("%d\n",dp[Lens]);
}
}
return 0;
}

最新文章

  1. MFC 文件对话框
  2. hdu4923 Room and Moor
  3. 常见的sql语句 注意点及用法【区分mysql 和Sqlserver】
  4. operator
  5. [Unity菜鸟] Unity Web Player 相关问题 (待完善)
  6. 关于android:screenOrientation=&quot;portrait&quot;等
  7. Oracle EBS-SQL (MRP-3):检查例外信息查询_建议取消_采购订单.sql
  8. 漫话Unity3D(三)
  9. Linux网络设备驱动(一) _驱动模型
  10. UWP自定义RadioButton实现Tab底部导航
  11. JSON数组形式字符串转换为List&lt;Map&lt;String,String&gt;&gt;的8种方法
  12. python练习题-day12
  13. Python requests库如何下载一个图片资源
  14. error: &#39;Can&#39;t connect to local MySQL server through socket &#39;/data/3307/data/mysql.sock&#39; (2)&#39;
  15. python中argparse模块用法实例详解
  16. 自己实现字符串转整数(不使用JDK的字符串转整数的方法)
  17. 10.18正式开发stark组件*(三)
  18. zk 创建瞬时、顺序节点的原理
  19. SpringSecurity学习之基于数据库的用户认证
  20. Eclipse+Pydev 搭建开发环境(转)

热门文章

  1. svn提交时提示 Aborting commit: remains in conflict 解决办法,更改svn服务地址
  2. 安卓和ios的app证书过期的相关问题汇总
  3. 工作三年!全靠大佬的Java笔记,年底跳槽阿里涨了10K
  4. jmeter性能测试-高并发分布式部署
  5. maven 报错 Failed to execute goal on project ...: Could not resolve dependencies for project ...
  6. 使用CDN后如何配置Apache使其记录访客真实IP
  7. java instanceof 判断是否是String
  8. 【vue-1】vue-cli3.0以上的搭建与配置(2.X的版本是不一样的)
  9. 个人总结的一些C++基础理论
  10. Mysql 实战关于date,datetime,timestamp类型使用