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的范围恰好又是常见状 ...
随机推荐
- 优雅的封装ajax,含跨域
之前写过一篇 先定一个小目标,自己封装个ajax,是基于原生js的,也就是jquery中ajax的简化版本实现的思路.众所周知,jquery的ajax是项目中最常用的请求后台的方式,也算是封装的很完美 ...
- Android源码博文集锦2
Android精选源码 android简单易用的Gallery android漂亮的加载效果 这可能是RxJava 2.x 最好的入门教程示例代码 android图片可拖拽排序 android用几行代 ...
- JavaScript+svg绘制的一个饼状图
结果: svg参考:https://www.w3.org/TR/SVG/<body onload='document.body.appendChild( pieChart([12,23,34,4 ...
- POJ 3279 枚举(思维)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10931 Accepted: 4029 Descrip ...
- pc端的企业网站(IT修真院test8)详解1-3
一,base.css基础样式表的意义 我昨天,整理了一下代码规范. 发现现在这个程度的页面还原.有必要创建一个规范的base.css库和framework.css库 而且也要为日后的工作整理一些常用的 ...
- 手机摄像头扫描识别车牌号,移动端车牌识别sdk
一.移动端车牌识别应用背景 (技术交流:18701686857 QQ:283870550) 随着经济水平的不断提高,汽车数量的不断激增为汽车管理带来了不小的难度.路边违章停车的现象越来越频繁.现在, ...
- (转)$.extend()方法和(function($){...})(jQuery)详解
1. JS中substring与substr的区别 之前在项目中用到substring方法,因为C#中也有字符串的截取方法Substring方法,当时也没有多想就误以为这两种方法的使用时一样的. ...
- (转)spring学习之@ModelAttribute运用详解
@ModelAttribute使用详解 1 @ModelAttribute注释方法 例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被 ...
- 【JAVASCRIPT】React + Redux
摘要 Redux 数据流图 View 层由React 控制, 根据state 变化 刷新渲染组件,作用是根据更新的数据重新渲染组件 Stroe 层其实就是state存储器,作用是更新数据 Dispat ...
- 37. leetcode 108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...