题目链接

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 ;
}
/*
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*/

最新文章

  1. SQLite的基本使用
  2. monads-are-elephants(转)
  3. [django]在virtualenv下安装的第三方库的使用方法
  4. 网页兼容浏览器测试工具Multibrowser
  5. Python自动化运维工具fabric的安装
  6. Kafka实战系列--Kafka API使用体验
  7. java中的xml与实体类之间的映射
  8. html背景自动移动
  9. poj 2778 DNA Sequence AC自动机
  10. js中的SetTimeOut
  11. 简学Python第七章__class面向对象高级用法与反射
  12. 远程SSH连接服务与基本排错
  13. jQuery表单事件
  14. CSS3 background-size 属性
  15. 聊聊我的 Java 自学之路
  16. Tomcat优化配置
  17. MaterialEditText——Android Material Design EditText控件
  18. 洛谷 P4148 简单题 解题报告
  19. MDX Cookbook 02 - 除数为零的问题
  20. EF-一对一关系

热门文章

  1. Java自学手记——注解
  2. Android反编译odex然后重新打包
  3. 关于SurfaceView的那些事
  4. 使用Vue-resource完成交互
  5. 【DG】Oracle_Data_Guard官方直译
  6. C++第三篇--程序结构
  7. vue.js中使用Axios
  8. 微信小程序开发基础知识总结
  9. javascript对象的创建--相对java 怎样去创建了&quot;类&quot;i以及实例化对象
  10. dubbo-zookeeper(续)