2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)
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
题意:
给定两个字符串,一个是主串,另一个模拟串,主串中只含有大小写字符,模拟串中除了含有大小写字符外,还有'.'和'','.'可以与主串中的任意的字符匹配,''可以将它前面的一个字符扩展或则删去,问这两个字符串是否能够匹配成功。
分析:
str表示主串,str1表示模拟串,可以假设dp[i][j]表示str1[1,i]与str[1,j]是否匹配。
显然dp[0][0] = true,都没有开始的时候默认是匹配的。
如果 str1[i] == . 或者str1[i] == str[j]时,dp[i][j] 的状态取决于状态dp[i-1][j-1]
如果str1[i] == ‘*‘时,因为这个字符可以可以延伸或则删除前一个 dp[i][j] == dp[i-1][j] | dp[i-2][j],
而当(dp[i-1][j-1] || dp[i][j-1]) && str[j-1] == str[j] 时,dp[i][j]必定为true;
具体的看一下代码把:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 2510;
char str[N], str1[N];
bool dp[N][N];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
memset(dp, false, sizeof(dp));
scanf("%s %s",str+1, str1+1);
int len = strlen(str+1), len1 = strlen(str1+1);
dp[0][0] = true;
for(int i = 1; i <= len1; i ++)
{
if(i == 2 && str1[i] == '*') dp[i][0] = true;///这样的话相当于完全可以将模拟串之前的全部去掉
for(int j = 1; j <= len; j ++)
{
if(str1[i] == '.' || str1[i] == str[j])///模拟串是点或者模拟串与主串的字符相等,匹配与否取决于每个串前一个字符
dp[i][j] = dp[i-1][j-1];
else if(str1[i] == '*')///模拟串是’*‘的话
{
dp[i][j] = dp[i-2][j] | dp[i-1][j];///模拟串看前一个是否与祖串匹配,或则去掉前一个之后是否与祖串匹配
if((dp[i-1][j-1] || dp[i][j-1]) && str[j-1] == str[j])///主串的当前位置与前一个位置相等,只要前面的位置匹配或者
dp[i][j] = true;
}
}
}
printf("%s\n",dp[len1][len]?"yes":"no");
}
return 0;
}
2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)的更多相关文章
- 2017ACM暑期多校联合训练 - Team 7 1010 HDU 6129 Just do it (找规律)
题目链接 Problem Description There is a nonnegative integer sequence a1...n of length n. HazelFan wants ...
- 2017ACM暑期多校联合训练 - Team 6 1010 HDU 6105 Gameia (博弈)
题目链接 Problem Description Alice and Bob are playing a game called 'Gameia ? Gameia !'. The game goes ...
- 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)
题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...
- 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)
题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...
- 2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)
题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants ...
随机推荐
- ES6 学习1
https://www.jianshu.com/p/287e0bb867ae 1,let表示变量.const表示常量.let和const都是块级作用域.一个在函数内部,一个在代码块内部: const ...
- Task的运行原理和工作窃取
在net4.0以前,当调用ThreadPool.QueueUserWorkItem方法往线程池中插入作业时,会把作业内容(其实就是一个委托)放到线程池中的一个全局队列中,然后线程池中的线程按照先进先出 ...
- android获取view宽高的几种方法
在onCreate方法中我们通过mView.getWidth()和mView.getHeight()获取到的view的宽高都是0,那么下面几种方法就可以在onCreate方法中获取到view的宽高. ...
- mybatis的mapper参数传递
简单参数传递 简单参数传递是指: 传递单个基本类型参数,数字类型.String 传递多个基本类型参数 parameterType 属性可以省略: 传递单个基本类型参数 SQL语句中参数的引用名称并不 ...
- matplotlib删除地图投影上的等值线及风场
[前言]最近在编写一个气象应用程序,用来显示某一时刻某一地区的气温等值线和风场,程序主要用到了第三方库matplotlib及Basemap.在编写的过程中发现,如果不进行擦除操作直接绘制新的等值线或风 ...
- 【BZOJ 3652】大新闻 数位dp+期望概率dp
并不难,只是和期望概率dp结合了一下.稍作推断就可以发现加密与不加密是两个互相独立的问题,这个时候我们分开算就好了.对于加密,我们按位统计和就好了;对于不加密,我们先假设所有数都找到了他能找到的最好的 ...
- Day22-Django之Form组件验证
1. Django里面的Form专门用来做验证. 用Form创建一个类,把用户发来的数据放到request.POST里面发给这个类,这个类会帮忙做验证. 返回3个结果:是否验证成功了,所有的正确信息, ...
- 【BZOJ1054】移动玩具(搜索)
[BZOJ1054]移动玩具(搜索) 题面 BZOJ 洛谷 题解 这种小清新搜索题写出来好舒服啊. 要是原来的我来写代码肯定又臭又长吧.. #include<cstdio> #includ ...
- python之冒泡排序
重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成.def Bubble_Sort(lists): ...
- RadioButton如何隐藏和替换
http://q.cnblogs.com/q/60738/ 用Jquery写的,可以实现隐藏功能,代码如下: 1 <%@ Page Language="C#" AutoEve ...