[dp]Search 字符串匹配问题

Problem D Search Time Limit: 1000ms, Memory Limit:64MB Description When we search for something on internet, we always use two characters “” and “?”. ”” represents 0 or more lowercase letters,”?” r
摘要由CSDN通过智能技术生成

Problem D Search
Time Limit: 1000ms, Memory Limit:64MB
Description
When we search for something on internet, we always use two characters “” and “?”. ”” represents 0 or more lowercase letters,”?” represents 1 lowercase letter.
We would use the two wildcard characters, while we are not sure about the keywords. Suppose that many records are stored in our database, each of them consists of lowercase letters. Give you a keyword, could you tell me how many records match with the keyword?
For example: the keyword is “j*y*m*y?”,then “jiyanmoyu”,”jyanmoyu”,”jymyu” are all match with it.
Input
Test cases less than 20. The first line of each test case is the keyword, the second line is an integer n, represents n records stored in database (1<=n<=10000). The length of each record is not more than 50.
Output
For each line in the input, there should be one line of output contains an integer representing the number of the records matching with keyword.
Sample input
jiyanmoyu
2
jiyanmoyu
huyanluanyu
ji*moy?
3
jiyanmoyu
jimoyu
huyanluanyu
Sample output:
1
2

题意:给定一个关键字,从已有的字典中查出所有与关键字匹配的串,输出匹配的个数。其中关键字中可能含有’?’或’*’通配符。
‘*’可以匹配上0到任意个字符,’?’可以匹配上1个字符。

使用dp的写法。f[i][j]表示p的前i个字符串,与s的前j个字符串是否匹配
p为匹配串,s为带匹配串
如果p[i] 为* ,那么 f[i][j] = f[i-1][j] | f[i][j-1]);
即:f[i][j-1]成功匹配的话,*可匹配s[j-1],或者f[i-1][j]

匹配成功的话,*不匹配任何字符;
如果p[i]为?,那么 f[i][j] = f[i-1][j-1];
其他 f[i][j] = f[i-1][j-1] & (p[i-1] == s[j-1]);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值