HDU 6170----Two strings(DP)

本文介绍了一种基于动态规划的字符串匹配算法,用于判断两个字符串是否匹配。特别地,该算法能够处理包含特殊字符‘.’和‘*’的模式字符串,其中‘.’可以匹配任意字符,而‘*’表示前一个字符可以重复任意次。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

 

Problem 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
3
aa
a*
abb
a.*
abb
aab
 

 

Sample Output
yes
yes
no
 
 
题意:给了两个字符串,判断是否匹配。第一个串只包含小写和大写字符,第二个串包含小写、大写字符,还包括‘ . ’和' * ',' . ' 可以匹配任意一个字符,' * ' 表示' * '之前的字符可以重复多次,比如a*可以匹配a、aa、aa……以及空串(注:第二个串不会以' * '开始,也不会有两个连续的' * ')。
 
思路:考虑DP,每次根据1~i的b串能使a串到达哪些位置,进而推出1~i+1的b串能使a串到达哪些位置。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=2505;
char a[N],b[N];
int len1,len2;
int dp[N][N];

int main()
{
    int T; cin>>T;
    while(T--){
       scanf("%s%s",a+1,b+1);
       len1=strlen(a+1);
       len2=strlen(b+1);
       memset(dp,0,sizeof(dp));
       dp[0][0]=1;
       for(int i=1;i<=len2;i++)
       {
           if(b[i]=='.')
           {
              for(int j=0;j<=len1;j++)
              {
                  if(dp[i-1][j]) dp[i][j+1]=1;
              }
           }
           else if(b[i]=='*')
           {
              for(int j=0;j<=len1;j++)
              {
                  if(dp[i-1][j])
                  {
                     dp[i][j]=1;
                     dp[i][j-1]=1;
                     while(a[j+1]==a[j]) dp[i][j+1]=1,j++;
                  }
              }
           }
           else
           {
              for(int j=0;j<=len1;j++)
              {
                  if(!dp[i-1][j]) continue;
                  if(a[j+1]==b[i]) dp[i][j+1]=1;
                  else if(b[i+1]=='*') dp[i+1][j]=1;
              }
           }
       }
       if(dp[len2][len1]) puts("yes");
       else puts("no");
    }
    return 0;
}
/*
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*/

比赛中我用的深搜模拟的,会超时,但是如果答案是"yes"的话,会很快的计算出,不会超时;如果是” no "的话,会搜索所有的情况,会超时,这个时候我们可以用一个变量记录一下递归次数,当大于一定次数时默认为“no”的情况,退出搜索。(当然这种做法不是正解,脑洞大开,如果有厉害的数据肯定过不了~)

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=2505;
char a[N],b[N];
int len1,len2;
int h[N];
int c;
int dfs(int i,int j)
{
    c++;
    if(c>1000000) return 0;///默认为"no"的情况;
    if(i<len1 && j>=len2) return 0;
    if(i>=len1){
        if(j>=len2) return 1;
        if(j==len2-1 && b[j]=='*') return 1;
        if(j==len2-1 && b[j]!='*') return 0;
        if(j<len2-1){
            if(b[j]=='*' && h[j+1]) return 1;
            else if(b[j]!='*' && h[j]) return 1;
            else return 0;
        }
    }
    if(b[j]=='.') { b[j]=a[i]; int f=dfs(i+1,j+1); b[j]='.'; return f; }
    if(b[j]=='*') {
        if(a[i]==b[j-1]){
            if(dfs(i+1,j)) return 1;
            if(dfs(i,j+1)) return 1;
            if(dfs(i-1,j+1)) return 1;
         }
        else {
            if(dfs(i-1,j+1)) return 1;
            if(dfs(i,j+1)) return 1;
        }
    }
    if(a[i]==b[j]) return dfs(i+1,j+1);
    else if(b[j+1]=='*') return dfs(i,j+2);
    else return 0;
}

int main()
{
    int T; cin>>T;
    while(T--){
       scanf("%s%s",a,b);
       c=0;
       len1=strlen(a);
       len2=strlen(b);
       int flag=1;
       for(int i=len2-1;i>=0;i--)
       {
           if(!flag) h[i]=0;
           else if(b[i]=='*'){
              h[i]=1; h[i-1]=1; i--;
           }
           else{
              h[i]=0;
              flag=0;
           }
       }
       int ans=dfs(0,0);
       if(ans) puts("yes");
       else puts("no");
    }
    return 0;
}
/*
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*/

 

 

转载于:https://www.cnblogs.com/chen9510/p/7421662.html

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值