题目链接

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

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

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=;
char a[N],b[N];
int len1,len2;
int h[N];
int c;
int dfs(int i,int j)
{
c++;
if(c>) return ;///默认为"no"的情况;
if(i<len1 && j>=len2) return ;
if(i>=len1){
if(j>=len2) return ;
if(j==len2- && b[j]=='*') return ;
if(j==len2- && b[j]!='*') return ;
if(j<len2-){
if(b[j]=='*' && h[j+]) return ;
else if(b[j]!='*' && h[j]) return ;
else return ;
}
}
if(b[j]=='.') { b[j]=a[i]; int f=dfs(i+,j+); b[j]='.'; return f; }
if(b[j]=='*') {
if(a[i]==b[j-]){
if(dfs(i+,j)) return ;
if(dfs(i,j+)) return ;
if(dfs(i-,j+)) return ;
}
else {
if(dfs(i-,j+)) return ;
if(dfs(i,j+)) return ;
}
}
if(a[i]==b[j]) return dfs(i+,j+);
else if(b[j+]=='*') return dfs(i,j+);
else return ;
} int main()
{
int T; cin>>T;
while(T--){
scanf("%s%s",a,b);
c=;
len1=strlen(a);
len2=strlen(b);
int flag=;
for(int i=len2-;i>=;i--)
{
if(!flag) h[i]=;
else if(b[i]=='*'){
h[i]=; h[i-]=; i--;
}
else{
h[i]=;
flag=;
}
}
int ans=dfs(,);
if(ans) puts("yes");
else puts("no");
}
return ;
}
/*
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*/

HDU 6170----Two strings(DP)的更多相关文章

  1. HDU 6170 Two strings (dp)

    /** * 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6170 * 字符串match, '.'代表匹配任意一个字符,"*" 代表 ...

  2. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  3. HDU 5791:Two(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Problem Description   Alice gets two sequences A ...

  4. HDU 4833 Best Financing(DP)(2014年百度之星程序设计大赛 - 初赛(第二轮))

    Problem Description 小A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在第dates[ ...

  5. hdu 4055 Number String(dp)

    Problem Description The signature of a permutation is a string that is computed as follows: for each ...

  6. HDU 4833 Best Financing (DP)

    Best Financing Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 1422 重温世界杯(DP)

    点我看题目 题意 : 中文题不详述. 思路 : 根据题目描述及样例可以看出来,如果你第一个城市选的是生活费减花费大于等于0的时候才可以,最好是多余的,这样接下来的就算是花超了(一定限度内的花超),也可 ...

  8. HDU 1176 免费馅饼(DP)

    点我看题目 题意 : 中文题.在直线上接馅饼,能接的最多是多少. 思路 :这个题其实以前做过.....你将这个接馅饼看成一个矩阵,也不能说是一个矩阵,反正就是一个行列俱全的形状,然后秒当行,坐标当列, ...

  9. Codeforces 543C Remembering Strings(DP)

    题意比较麻烦 见题目链接 Solution: 非常值得注意的一点是题目给出的范围只有20,而众所周知字母表里有26个字母.于是显然对一个字母进行变换后是不影响到其它字符串的. 20的范围恰好又是常见状 ...

随机推荐

  1. mysql表生成JavaBean

    MySQLToBean.java package org.just.util; import java.io.File; import java.io.FileInputStream; import ...

  2. Java Jpa 规范

    Jpa最早是EJB3.0里面的内容,JSR 220: Enterprise JavaBeansTM 3.0 https://www.jcp.org/en/jsr/detail?id=220 后来大约在 ...

  3. Work 3(工作类) (2017.07.01)

  4. openjudge8465:马走日 [搜索]

    描述 马在中国象棋以日字形规则移动. 请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点. 输入 第一行为整数T ...

  5. ES6函数扩展

    前面的话 函数是所有编程语言的重要组成部分,在ES6出现前,JS的函数语法一直没有太大的变化,从而遗留了很多问题和的做法,导致实现一些基本的功能经常要编写很多代码.ES6大力度地更新了函数特性,在ES ...

  6. MySQL学习笔记(三):常用函数

    一:字符串函数 需要注意的几个细节: 1.cancat中有一个字符串为null,则结果为null. 2.left(str,x) 和 right(str,x)中x为null,则不返回任何字符串,不是nu ...

  7. node 简介 起源

    最近的node 的一篇文章阅读量很大,所以写一篇基础篇供大家分享,抛砖引玉,各取所需. 部分内容来源大神笔记. Node.js 简介:@@@@@@@@@@@@@@@@@@@ Node.js是目前非常火 ...

  8. javascript编码规范总结

    1.嵌入规则 Javascript程序应该尽量放在.js的文件中,需要调用的时候在页面中以<script src="filename.js">的形式包含进来.Javas ...

  9. EXchange导出通讯录提取url纯文本

    用outlook链接邮箱 文件-打开和导出--导出到文件--逗号分隔值--选择联系人--保存 保存为一个后缀为csv的文件 打开该文件  选中该列 用替换功能删掉()符号 用vba脚本删掉汉字 Sub ...

  10. 为Tornado框架加上基于Redis或Memcached的session 【第三方】

    Tornado 没有session,只有cookie_secret,这在一些情况下是不利于开发的.所有我们可以给Tornado加上session的功能. 这篇文章讲解的是依靠第三方包来实现.以后的文章 ...