zoukankan      html  css  js  c++  java
  • (全国多校重现赛一) J-Two strings

    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

    3
    aa
    a*
    abb
    a.*
    abb
    aab

    Sample Output

    yes
    yes
    no

    题意:给两个字符串,a固定,b由英文字符和*和.组成,“.”可以变成任意字符,“*”和前一字符一样,且前一个字符的数量为任意多个;求是否可让b变成和a一样‘;

    题解:DP;dp[i][j]表示字符串b从1~i与a从1~j是否完全匹配;

    由于字符不为空,且第一个字符不为*,没连续的*;

    如果b第二个为“*”,的话dp[2][0]=1;

    如果b[i]==a[j] ,则dp[i][j]可以由dp[i-1][j-1]转化而来;

    如果b[i]==".",则b[i]可以为任何字符,dp[i][j]可有由dp[i-1][j-1]转化而来;

    如果b[i]=="*": 

               1.如果匹配0则dp[i][j]由dp[i-2][j]转化而来,如果匹配1个,dp[i][j]=dp[i-1][j];

               2.如果匹配多个 dp[i][j] = max(dp[i][j],max(dp[i][j-1],dp[i-1][j-1]));

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int mx = 3e3+10;
    int n,m,len1,len2;
    char str[mx],stc[mx];
    int dp[mx][mx];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
    	{
            scanf("%s",str+1);
            scanf("%s",stc+1);
            len1 = strlen(str+1);
            len2 = strlen(stc+1);
            memset(dp,0,sizeof(dp));
            dp[0][0] = 1;
            for(int i=1;i<=len2;i++)
    		{
                if(i==2&&stc[i]=='*') dp[i][0]  = 1;
                for(int j=1;j<=len1;j++)
    			{
                    if(isalpha(stc[i]))
    				{
                        if(str[j]==stc[i]) dp[i][j] = dp[i-1][j-1];
                    }
    				else if(stc[i]=='.') dp[i][j] = dp[i-1][j-1];
    				else
    				{
                        dp[i][j] = max(dp[i][j],max(dp[i-1][j],dp[i-2][j]));
                        if((dp[i][j-1]||dp[i-1][j-1])&&str[j]==str[j-1])
                        dp[i][j] = max(dp[i][j],max(dp[i][j-1],dp[i-1][j-1]));
                    }
                }
            }
            puts(dp[len2][len1]?"yes":"no");
        }
        return 0;
    }
    
  • 相关阅读:
    Linux下查看网卡驱动和版本信息
    HTML <!--...--> 标签
    linux物理网卡检测命令mii-tool
    Git错误non-fast-forward后的冲突解决
    ubuntu上解决访问github慢的方法
    右侧添加悬浮打赏功能
    VSCode 预览 .md 文件
    Matlab绘制三维曲面(以二维高斯函数为例)
    matlab的三维绘图和四维绘图
    ubuntu查看文件和文件夹大小
  • 原文地址:https://www.cnblogs.com/csushl/p/9386497.html
Copyright © 2011-2022 走看看