HDU 6170 Two strings (dp)

网友投稿 579 2022-10-03

HDU 6170 Two strings (dp)

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 using namespace std;const int maxn=2600;char a[maxn],b[maxn];bool dp[maxn][maxn];int lena,lenb;int main(){ int T; scanf("%d%*c",&T); while(T--) { memset(dp,false,sizeof(dp)); a[0]=b[0]=1; gets(a+1); gets(b+1); lena = strlen(a) - 1; lenb = strlen(b) - 1; dp[0][0]=true; for(int i=1; i<=lenb; i++) { if(i>=2&&b[i]=='*') dp[i][0] |= dp[i-2][0]; for(int j=1; j<=lena; j++) { if(b[i]=='.'||a[j]==b[i]) dp[i][j]=dp[i-1][j-1]; else if(b[i]=='*') { dp[i][j]=dp[i-2][j]|dp[i-1][j]; if((dp[i-1][j-1]||dp[i][j-1])&&a[j-1]==a[j]) dp[i][j] = true; } } } puts(dp[lenb][lena]?"yes":"no"); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序消息推送配置案例(小程序消息推送配置怎么填写)
下一篇:详解SpringBoot整合RabbitMQ如何实现消息确认
相关文章

 发表评论

暂时没有评论,来抢沙发吧~