zoukankan      html  css  js  c++  java
  • HDU 6170 Two strings

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6170

    题意:给出2个字符串,判断能否匹配

    第一个字符串为大小写字母,第二个字符串中还有两种符号,‘ .’可以匹配任意字符,‘ *’表示前一个字符可以使用0次或多次

    设dp[i][j]为a串的前i个字符可以与b串的前j个字符匹配,之后分情况判断

    特别要注意这组数据

    1
    aaas
    .as*.* 
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #define lson i<<1
    #define rson i<<1|1
    using namespace std;
    char a[3000],b[3000];
    bool dp[2505][2505];
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%s%s",a+1,b+1);
            int lena=strlen(a+1);
            int lenb=strlen(b+1);
            memset(dp,0,sizeof(dp));
            dp[0][0]=1;
            for(int j=1;j<=lenb;j++)
            {
                if (b[j]=='*'&&j==2) dp[0][j]=1;
                for(int i=1;i<=lena;i++)
                {
                    if (b[j]==a[i]||b[j]=='.')
                        dp[i][j]=dp[i-1][j-1];
                    else if (b[j]=='*')
                    {
                        dp[i][j]=max(dp[i][j-1],dp[i][j-2]);
                        if (a[i-1]==a[i]) dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
                        /*
                        或者
                        if (dp[i-1][j])
                        {
                            if (b[j-1]==a[i]||(b[j-1]=='.'&&a[i-1]==a[i])) dp[i][j]=1;
                        }
                        */
                    }
                }
            }
            if (dp[lena][lenb]) printf("yes
    ");
            else printf("no
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    js操作class值
    四、多表连接
    三、约束
    根据出生日期计算年龄
    二、MySql数据操作(二)
    一、MySql基本语句(一)
    jquery基本操作
    纯js实现全选,全不选,反选
    css的基础知识1
    表格的使用
  • 原文地址:https://www.cnblogs.com/bk-201/p/7417955.html
Copyright © 2011-2022 走看看