HDU 6170----Two strings(DP)
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 “*”.
For each test case, there are two lines implying the two strings (The length of the two strings is less than 2500).
#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)的更多相关文章
- HDU 6170 Two strings (dp)
/** * 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6170 * 字符串match, '.'代表匹配任意一个字符,"*" 代表 ...
- 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 ...
- HDU 5791:Two(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Problem Description Alice gets two sequences A ...
- HDU 4833 Best Financing(DP)(2014年百度之星程序设计大赛 - 初赛(第二轮))
Problem Description 小A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在第dates[ ...
- hdu 4055 Number String(dp)
Problem Description The signature of a permutation is a string that is computed as follows: for each ...
- HDU 4833 Best Financing (DP)
Best Financing Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1422 重温世界杯(DP)
点我看题目 题意 : 中文题不详述. 思路 : 根据题目描述及样例可以看出来,如果你第一个城市选的是生活费减花费大于等于0的时候才可以,最好是多余的,这样接下来的就算是花超了(一定限度内的花超),也可 ...
- HDU 1176 免费馅饼(DP)
点我看题目 题意 : 中文题.在直线上接馅饼,能接的最多是多少. 思路 :这个题其实以前做过.....你将这个接馅饼看成一个矩阵,也不能说是一个矩阵,反正就是一个行列俱全的形状,然后秒当行,坐标当列, ...
- Codeforces 543C Remembering Strings(DP)
题意比较麻烦 见题目链接 Solution: 非常值得注意的一点是题目给出的范围只有20,而众所周知字母表里有26个字母.于是显然对一个字母进行变换后是不影响到其它字符串的. 20的范围恰好又是常见状 ...
随机推荐
- voa 2015 / 4 / 26
Now, Words and Their Stories, a VOA Special English program about American expressions. I'm Rich Kle ...
- maven 打包时mapper.xml打不进去问题
首先,来看下MAVENx项目标准的目录结构: 一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,ma ...
- epii.js简约而不简单的JS模板引擎
epii.js是什么 epii.js是一个 模板引擎,可快速实现数据与ui绑定,快速实现事件绑定,与处理,不依赖任何第三方库,仅仅8k,在native+webapp开发 和 web开发,h5微网页上均 ...
- iOS 实现类似QQ分组样式的几种方式
思路 思路很简单,对模型数据操作或则控制界面显示 先看下json部分数据 "chapterDtoList": [{ "token": null, "i ...
- pc端的企业网站(IT修真院test8)详解1-3
一,base.css基础样式表的意义 我昨天,整理了一下代码规范. 发现现在这个程度的页面还原.有必要创建一个规范的base.css库和framework.css库 而且也要为日后的工作整理一些常用的 ...
- anaconda 下多版本Python 安装说明
网上针对多版本的Python兼容安装的文章逐渐增多,都是大家在实践中总结的经验.本人的安装经过几次的反复实验还是觉得其中一种更为方便. 有人的安装方法是: 1. 先安装一个版本的python(一般先安 ...
- maven - 引用本地jar,进行jar包移动
背景: 项目为maven工程,部分jar需要需用项目单独修改的本地jar包. 配置好scope后发现构建后引用的jar没有移动到对应的目录,百度后发现需要配置以下依赖 <plugin> & ...
- 运行shell脚本时报错"[[ : not found"解决方法
问题描述 在运行shell脚本时报错,命令为: sh test.sh 报错如图: 脚本代码如下: #!/bin/bash # file:test.sh # author:13 # date:2017- ...
- Angular02 将数据添加到组件中
准备:已经搭建好angular-cli环境.知道如何创建组件 一.将一个数据添加到组件中 1 创建一个新的组件 user-item 2 将组件添加到静态模板中 3 为组件添加属性,并利用构造器赋值 4 ...
- 机器学习(3)-Tensorflow安装与测试
安装.# Ubuntu/Linux 64-bit $ sudo apt-get install python-pip python-dev # Ubuntu/Linux 64-bit, CPU onl ...