[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
    评论
字符串匹配问题可以使用动态规划法进行解决。具体来说,可以使用一个二维数组dp[i][j]表示文本串中前i个字符与模式串中前j个字符是否匹配。其中,dp[0][0]表示两个空串匹配,dp[i][0]表示空模式串与任何文本串都匹配,dp[0][j]则表示文本串为空,只有当模式串中前j个字符均为*时才为匹配。 状态转移方程如下: 1. 当模式串第j个字符是*时: dp[i][j] = dp[i][j-1] || dp[i-1][j] 解释:*可以表示空字符或者匹配一个或多个字符。dp[i][j-1]表示*匹配0个字符,dp[i-1][j]表示*匹配一个或多个字符。 2. 当模式串第j个字符不是*时: dp[i][j] = dp[i-1][j-1] && (text[i] == pattern[j] || pattern[j] == '?') 解释:当模式串第j个字符不是*时,只有当文本串中第i个字符与模式串中第j个字符相同或者模式串中第j个字符为?时才能匹配。 最终的匹配结果为dp[text_len][pattern_len],其中text_len和pattern_len分别为文本串和模式串的长度。 以下是使用C语言实现字符串匹配问题的动态规划法的示例代码: ```c #include <stdio.h> #include <string.h> #define MAX_LEN 1000 int dp[MAX_LEN][MAX_LEN]; int isMatch(char* text, char* pattern) { int text_len = strlen(text); int pattern_len = strlen(pattern); // 初始化dp数组 memset(dp, 0, sizeof(dp)); dp[0][0] = 1; // 处理dp数组的第一行 for (int j = 1; j <= pattern_len; j++) { if (pattern[j-1] == '*') { dp[0][j] = dp[0][j-1]; } } // 处理dp数组的其余部分 for (int i = 1; i <= text_len; i++) { for (int j = 1; j <= pattern_len; j++) { if (pattern[j-1] == '*') { dp[i][j] = dp[i][j-1] || dp[i-1][j]; } else { dp[i][j] = dp[i-1][j-1] && (text[i-1] == pattern[j-1] || pattern[j-1] == '?'); } } } // 返回匹配结果 return dp[text_len][pattern_len]; } int main() { char text[MAX_LEN], pattern[MAX_LEN]; printf("请输入文本串:"); scanf("%s", text); printf("请输入模式串:"); scanf("%s", pattern); if (isMatch(text, pattern)) { printf("匹配成功!\n"); } else { printf("匹配失败!\n"); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值