【DP】HDU 6170/2017多校9 1010 Two strings
来源:互联网 发布:节奏大师没有网络可用 编辑:程序博客网 时间:2024/06/06 01:11
题目:
HDU 6170
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
分析:
较简单的DP f[i][j]
为a串前i个与b串前j个是否匹配 1:匹配 0:不匹配
先将b串的’*’的前一个字符单独记录下来(p数组中),然后删掉,成为新的b串
f[i][j]=f[i-1][j-1]&&(a[i]==b[j]);
b[j]为字母f[i][j]=f[i-1][j-1];
b[j]为’.’f[i][j]=f[i][j]||f[i-1][j]||f[i-1][j-1]; fla[i][j]=1;
b[j]为’*’且p[j]=’.’且fla[i][j]=0;f[i][j]=f[i][j]||f[i-1][j]&&(a[i]==a[i-1]);
b[j]为’*’且p[j]=’.’且fla[i][j]=1;f[i][j]=f[i][j-1]||(f[i-1][j]&&(a[i]==p[j]));
b[j]为’*’且p[j]为字母fla[i][j]
用来标记”.*”中的’.’是否已经与某个字符匹配
(”.*”必须与相同的多个字符串匹配,如”aaaaa“或”bbb“,不能是”absaf“,因为题意不清WA了一发 = =)
想清楚后,代码很easy
#include<iostream>#include<cstdio>#include<cmath>#include<map>#include<algorithm>#include<cstring> #include<vector> using namespace std;const int maxn=2600;char a[maxn],b[maxn],p[maxn];int la,lb,t,tmp,f[maxn][maxn];bool fla[maxn][maxn];void pre (){ int i,j; la=strlen (a+1); tmp=strlen (b+1); lb=0; for (i=1;i<=tmp;i++){ if (b[i+1]!='*'){ b[++lb]=b[i]; } else { p[++lb]=b[i]; b[lb]='*'; i++; } }}void work (){ int i,j,k; memset (f,0,sizeof (f)); memset (fla,0,sizeof (fla)); f[0][0]=1; for (i=1;i<=lb;i++){ if (f[0][i-1]){ if (b[i]=='*')f[0][i]=1; } else break; } for (i=1;i<=la;i++){ for (j=1;j<=lb;j++){ if (b[j]>='a'&&b[j]<='z'||b[j]>='A'&&b[j]<='Z'){ f[i][j]=f[i-1][j-1]&&(a[i]==b[j]); } else if (b[j]=='.'){ f[i][j]=f[i-1][j-1]; } else { if (p[j]=='.'){ f[i][j]=f[i][j-1]; if (f[i][j])continue; if (!fla[i-1][j]){//没有匹配 f[i][j]=f[i][j]||f[i-1][j]||f[i-1][j-1]; fla[i][j]=1; } else { f[i][j]=f[i][j]||f[i-1][j]&&(a[i]==a[i-1]); } } else { f[i][j]=f[i][j-1]||(f[i-1][j]&&(a[i]==p[j])); } } } } if (f[la][lb]){ printf ("yes\n"); } else printf ("no\n");}int main (){ scanf ("%d",&t); while (t--){ scanf ("%s%s",a+1,b+1); pre (); work (); } return 0;}
- 【DP】HDU 6170/2017多校9 1010 Two strings
- HDU 6170 / 多校1010 Two strings (dp)
- 2017多校第9场 HDU 6170 Two strings DP
- hdu 6170 Two strings dp
- hdu 6170 Two strings(DP)
- HDU 6170(Two strings-DP)
- hdu 6170 Two strings(dp)
- HDU 6170 && 2017 多校训练:Two strings(DP)
- hdu 6170 Two strings dp模拟
- Hdu 6170 Two strings【思维+Dp】
- HDU 6170 Two strings (dp)
- HDU 6170 Two strings (二维DP)
- hdu 6170 Two strings(线性dp)
- HDU 6170 Two strings(dp)
- HDU 6170 Two strings dp || 正则
- hdu 6170 Two strings (dp)
- HDU 6170 Two strings (DP)
- HDU 6170 Two strings(DP)
- 封装数据库查询方法
- 表单标签<form>
- POJ3041 二分图最小点覆盖(匈牙利算法)
- android 文字的处理
- 在分布式配置K3中间层时报中间层创建事务失败 处理方法
- 【DP】HDU 6170/2017多校9 1010 Two strings
- static的作用
- 学生信息管理系统之实时错误3021
- The Super Powers
- Qt编程公众号二维码
- 树状数组的三种模式
- poj 3026
- Xutils概述
- 放假两个月,我几乎没碰过代码,你也和我一样在面对迷茫吗?