OPENJUDGE NOI 6252 带通配符的字符串匹配

来源:互联网 发布:js 遍历div中的ul li 编辑:程序博客网 时间:2024/06/05 05:25

描述
通配符是一类键盘字符,当我们不知道真正字符或者不想键入完整名字时,常常使用通配符代替一个或多个真正字符。通配符有问号(?)和星号()等,其中,“?”可以代替一个字符,而“”可以代替零个或多个字符。
你的任务是,给出一个带有通配符的字符串和一个不带通配符的字符串,判断他们是否能够匹配。
例如,1?456 可以匹配 12456、13456、1a456,但是却不能够匹配23456、1aa456;
2*77?8可以匹配 24457798、237708、27798。


【题目分析】
动态规划。


【代码】

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;char s1[25],s2[25];int l1,l2;int dp[25][25];int main(){    scanf("%s%s",s1+1,s2+1);    l1=strlen(s1+1); l2=strlen(s2+1);    dp[0][0]=1;    for (int i=0;i<=l1;++i)        for (int j=0;j<=l2;++j)            if ((i*j)&&(s1[i]==s2[j]||s1[i]=='?')) dp[i][j]|=dp[i-1][j-1];            else if (s1[i]=='*')                for (int k=j;k>=0;--k)                    dp[i][j]|=dp[i-1][k];    if (dp[l1][l2]) printf("matched\n");    else printf("not matched\n");}
0 0