(全国多校重现赛一) 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 题意: 鉴纯夏是一名成绩不太好的高中生. ...
随机推荐
- 深入理解计算机系统 第八章 异常控制流 Part2 第二遍
第二遍读这本书,每周花两到三小时时间,能读多少读多少(这次看了第 508~530 页,共 23 页) 第一遍对应笔记链接 https://www.cnblogs.com/stone94/p/10206 ...
- webpack安装与核心概念
安装webpack webpack核心概念:入口.输出.加载器.插件.模块.模式 一.安装webpack 1.安装webpack之前需要安装nodejs环境,在使用nodejs环境自带的包管理工具np ...
- opencv 2 Opencv数据结构与基本绘图
基础图像容器Mat Mat 是一个类,又两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩阵(根据所选存储方法不同,矩阵可以是不同的维数)的指针.矩阵头的尺 ...
- Tarjan-割点
割点——tarjan #include <bits/stdc++.h> using namespace std; ; ; int n, m; int ans;//个数 * MAXM], n ...
- .NET做人脸识别并分类
.NET做人脸识别并分类 在游乐场.玻璃天桥.滑雪场等娱乐场所,经常能看到有摄影师在拍照片,令这些经营者发愁的一件事就是照片太多了,客户在成千上万张照片中找到自己可不是件容易的事.在一次游玩等活动或家 ...
- 微信小程序-rpx
rpx 是微信小程序解决自适应屏幕尺寸的尺寸单位.微信小程序规定屏幕的宽度为750rpx. 微信小程序同时也支持rem尺寸单位, rem 规定屏幕的宽度为20rem, 所以 1rem = (750/2 ...
- nslookup命令查找域名
了解 DNS 域名服务 熟悉使用 nslookup 查找 DNS 服务器上登记的域名,记录几次查询的结果, 及服务器的 ip. 1. 某个子域下的一部分主机的名字- IP 地址对应关系,如 flame ...
- day 14 内置函数
复习了解: \t 输出一个制表符,协助在输出文本时,垂直方向保持对齐 \n 换行符 print(r"\n ") # 在字符串前面加r 不会改变字符串的内容 a ...
- scrapy实现自动抓取51job并分别保存到redis,mongo和mysql数据库中
项目简介 利用scrapy抓取51job上的python招聘信息,关键词为“python”,范围:全国 利用redis的set数据类型保存抓取过的url,现实避免重复抓取: 利用脚本实现每隔一段时间, ...
- if判断语句的总结
1.表达式:关系表达式或逻辑表达式: 2.表达式的运算结果应该是“真”或者“假”: 真:执行该语句: 假:跳过该语句,执行下一条语句: 3.“语句”可以是单语句也可以是复合语句: ...