题目链接

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. DELPHI XE5安装

    1.安装XE5 2. HNFJ-DPADCW-BDWCFU-FPNN QDF4-CTSDHV-RDFCFE-FEAN HNFK-BCN8NN-78N53D-H4RS 破解补丁使用方法: (1).复制压 ...

  2. mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...

  3. 事务之使用JDBC进行事务的操作

    本篇讲述数据库中非常重要的事务概念和如何使用MySQL命令行窗口来进行数据库的事务操作.下一篇会讲述如何使用JDBC进行数据库的事务操作. 事务是指数据库中的一组逻辑操作,这个操作的特点就是在该组逻辑 ...

  4. Ambari部署时问题之Ambari Metrics无法启动

    首先,我的问题是如下: Traceback (most recent call last): File , in <module> AMSServiceCheck().execute() ...

  5. poj_1845: Sumdiv

    题目链接 先将A^B分解质因数,可以通过先分解A,再把对应的幂次*B.之后用下面这个式子求解就可以了 #include<vector> #include<iostream> u ...

  6. java内存区域——深入理解JVM读书笔记

    本内容由<深入理解java虚拟机>的部分读书笔记整理而成,本读者计划连载. 通过如下图和文字介绍来了解几个运行时数据区的概念. 方法区:它是各个线程共享的区域,用于内存已被VM加载的类信息 ...

  7. (转载)Windows 上搭建Apache FtpServer

    因工作需要,最近经常接触到FTP,今天我来介绍一个开源的FTP服务器,那就是Apache FTPServer,Apache FTPServer是一个100%纯Java的FTP服务器. 它的设计是基于现 ...

  8. web前端开发面试题(未完待续)

    一.HTML与XHTML的不同:1)XHTML元素必须被正确地嵌套 2)元素必须被关闭   如:<h1>--</h1>关闭 3)标签名必须用小写字母 4)XHTML文档必须有根 ...

  9. ubuntu下使用 chkconfig 是一种习惯

    ubuntu下使用 chkconfig 是一种习惯 习惯了chkconfig命令, 闲来写了个脚本模拟下, 步骤很简单. 如下: 第一步, 安装sysv-rc-conf sudo apt instal ...

  10. 快速搭建MySQL复制集

    快速搭建MySQL复制集 1 环境说明 MySQL版本 5.6 basedir :/u01/my3306 #MySQL软件目录 数据目录 :/u01/mysql/[实例名]/data 日志目录 :/u ...