题目链接

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)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 7 1010 HDU 6129 Just do it (找规律)

    题目链接 Problem Description There is a nonnegative integer sequence a1...n of length n. HazelFan wants ...

  2. 2017ACM暑期多校联合训练 - Team 6 1010 HDU 6105 Gameia (博弈)

    题目链接 Problem Description Alice and Bob are playing a game called 'Gameia ? Gameia !'. The game goes ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

  7. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  8. 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)

    题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...

  9. 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 ...

随机推荐

  1. TiDB注意事项

    公司最近在上测试的TiDB集群,这款数据库类似MySQL,但又不完全一致,在使用的时候有一下注意事项,在这里记录一下.

  2. mac下面安装redis

    本文只记录了在homebrew下面安装redis的过程,过程比较简单,作为自己以后翻阅用吧, 首先安装homebrew,打开终端,在终端下面允许下面的命令(不要用root用户): /usr/bin/r ...

  3. 面试问题总结二(技术能力-PHP)----Ⅱ

    20.支付功能的实现? 答:在线支付一般来说有两种实现方式,一种是调用各个银行提供的接口,另一种是使用第三方集成好的支付功能,两种方式各有优劣.对于第三方支付来说会需要提交企业5证来验证,还会有部分手 ...

  4. sqlserver 对比数据库表是否完全一致的简单方法

    1. 使用数据库的工具进行处理 tablediff.exe 工具目录 C:\Program Files\Microsoft SQL Server\\COM 工具使用说明 tablediff.exe - ...

  5. Python基础【3】:Python中的深浅拷贝解析

    深浅拷贝 在研究Python的深浅拷贝区别前需要先弄清楚以下的一些基础概念: 变量--引用--对象(可变对象,不可变对象) 切片(序列化对象)--拷贝(深拷贝,浅拷贝) 我是铺垫~ 一.[变量--引用 ...

  6. html template & iframe

    html template & iframe https://bbs.csdn.net/topics/390123946 据说可以利用某些浏览器bug绕过跨域限制,可以也研究下; 由于浏览器对 ...

  7. 卸载Visual Studio最佳方法难道真的是重装系统?

    卸载Visual Studio最佳方法难道真的是重装系统? 卸载Visual Studio最佳方法难道真的是重装系统? 使用TotalUninstaller貌似也没有效果,默认卸载的,程序列表里面还是 ...

  8. 对HashMap的理解(三):ConcurrentHashMap

    HashMap不是线程安全的.在并发插入元素的时候,有可能出现环链表,让下一次读操作出现死循环.避免HashMap的线程安全问题有很多方法,比如改用HashTable或Collections.sync ...

  9. Qss 样式表的尝试

    QLineEdit{ border:1px solid #137eb6; padding:2px; background-color:#F5F5F5; } QToolTip{ border:1px s ...

  10. BZOJ5324 JXOI2018守卫(区间dp)

    对于每个区间[l,r],显然右端点r是必须放置守卫的.考虑其不能监视到的点,构成一段段区间.一个非常显然但我就是想不到的性质是,对于这样的某个区间[x,y],在(y+1,r)内的点都是不能监视到这个区 ...