洞察探讨小游戏SDK接入的最佳实践以及对企业跨平台开发的优势
617
2022-10-03
HDU 6170 Two strings (dp)
Description
Giving two strings and you should judge if they are matched.The first string contains lowercase letters and uppercase letters.The second string contains lowercase letters, uppercase letters, and special symbols: “.” and “*”.. can match any letter, and * means the front character can appear any times. For example, “a.b” can match “acb” or “abb”, “a*” can match “a”, “aa” and even empty string. ( “*” will not appear in the front of the string, and there will not be two consecutive “*”.
Input
The first line contains an integer T implying the number of test cases. (T≤15)For each test case, there are two lines implying the two strings (The length of the two strings is less than 2500).
Output
For each test case, print “yes” if the two strings are matched, otherwise print “no”.
Sample Input
3aaa*abba.*abbaab
Sample Output
yesyesno
题意
给出原串与匹配串,问能否匹配原串中所有的字符。
思路
如果这是一个标准的正则匹配,是不是可以直接用语言特性了呢?
我们设原串为 a ,匹配串为 b , dp[i][j] 代表 b[1..i] 与 a[1..j] 是否匹配成功。
显然 dp[0][0] = true ,
对于其他情况:
如果b[i] == '.' ,则此时 a[j] 可以是任意字符, dp[i][j] 由 dp[i-1][j-1] 转移而来。如果a[j] == b[i] ,同样 dp[i][j] 由 dp[i-1][j-1] 转移而来。如果b[i] == '*' ,假设该 * 最终可以匹配 0 位,则 dp[i][j] 状态从 dp[i-2][j] 转移而来,假设最终匹配 1 位,则从 dp[i-1][j] 转移而来;假如a[1..j-1] 与 b[1..i-1] 已成功匹配,并且 a[j-1] == a[j] ,显然当前的 * 可以继续匹配这一个字符,因此 dp[i][j] = true ;假如a[1..j-1] 与 b[1..i] 已成功匹配(当前 * 已成功匹配若干位),且 a[j-1] == a[j] ,则可以继续匹配这一个字符,因此 dp[i][j] = true 。特别的,如果b[i] == '*' ,则 dp[i][0] 可以从 dp[i-2][0] 转移而来,因此 dp[i][0] |= dp[i-2][0] 。
AC 代码
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~