(全国多校重现赛一) 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;
}
(全国多校重现赛一) J-Two strings的更多相关文章
- (全国多校重现赛一) H Numbers
zk has n numbers a1,a2,...,ana1,a2,...,an. For each (i,j) satisfying 1≤i<j≤n, zk generates a new ...
- (全国多校重现赛一)F-Senior Pan
Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory pro ...
- (全国多校重现赛一)D Dying light
LsF is visiting a local amusement park with his friends, and a mirror room successfully attracts his ...
- (全国多校重现赛一)E-FFF at Valentine
At Valentine's eve, Shylock and Lucar were enjoying their time as any other couples. Suddenly, LSH, ...
- (全国多校重现赛一)B-Ch's gifts
Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing a ...
- (全国多校重现赛一)A-Big Binary Tree
You are given a complete binary tree with n nodes. The root node is numbered 1, and node x's father ...
- 长春理工大学第十四届程序设计竞赛(重现赛)J.Printout
链接:https://ac.nowcoder.com/acm/contest/912/J 题意: 小r为了打校赛,他打算去打字社打印一份包含世界上所有算法的模板. 到了打字社,小r一看价格:总打印页数 ...
- 长春理工大学第十四届程序设计竞赛(重现赛)J
J.Printout 题目:链接:https://ac.nowcoder.com/acm/contest/912/J 题目: 小r为了打校赛,他打算去打字社打印一份包含世界上所有算法的模板. 到了打字 ...
- 2019CCPC秦皇岛赛区(重现赛)- J
链接: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1010&cid=872 题意: 鉴纯夏是一名成绩不太好的高中生. ...
随机推荐
- Okhttp3源码解析
首先是Okhttp的使用: //缓存文件夹 File cacheFile = new File(getExternalCacheDir().toString(), "cache") ...
- leetcode算法笔记:二叉树,动态规划和回溯法
在二叉树中增加一行 题目描述 给定一个二叉树,根节点为第1层,深度为 1.在其第 d 层追加一行值为 v 的节点. 添加规则:给定一个深度值 d (正整数),针对深度为 d-1 层的每一非空节点 N, ...
- 小白学 Python 爬虫(2):前置准备(一)基本类库的安装
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 本篇内容较长,各位同学可以先收藏后再看~~ 在开始讲爬虫之前,还是先把环境搞搞好,工欲善其事必先利其器嘛~~~ 本篇 ...
- (线段树)A Corrupt Mayor's Performance Art
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 题意: 区间更新, 区间询问: 题解: 区间更新, 区间询问, 一共30种颜色, 可用int 来 ...
- VS Code 之 Jupyter NoteBook 初试
一.前言 在今年九月的 PyCon China 大会上,官宣了一项 VS Code Python 的全新功能:Visual Studio Code Python 插件将提供 Jupyter Noteb ...
- ThreadLocal原理分析与代码验证
ThreadLocal提供了线程安全的数据存储和访问方式,利用不带key的get和set方法,居然能做到线程之间隔离,非常神奇. 比如 ThreadLocal<String> thread ...
- 构思一个在windows下仿objc基于动画层ui编程的ui引擎
用c/c++编程有些年了,十个指头可以数齐,在涉入iOS objc开发后,有种无比舒服的感觉,尤其在UI开发上. 在QuartzCore.framework下动画和透明窗口等许多效果的事都变得那么方便 ...
- MySQL锁会不会,你就差看一看
数据库锁知识 不少人在开发的时候,应该很少会注意到这些锁的问题,也很少会给程序加锁(除了库存这些对数量准确性要求极高的情况下),即使我们不会这些锁知识,我们的程序在一般情况下还是可以跑得好好的.因为这 ...
- tensorflow学习笔记——模型持久化的原理,将CKPT转为pb文件,使用pb模型预测
由题目就可以看出,本节内容分为三部分,第一部分就是如何将训练好的模型持久化,并学习模型持久化的原理,第二部分就是如何将CKPT转化为pb文件,第三部分就是如何使用pb模型进行预测. 一,模型持久化 为 ...
- JDK1.8新特性(一):stream
一.什么是stream? 1.概述 Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据. 这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管 ...