HDU6170 Two strings(dp,2017 HDU多校联赛 第9场)
来源:互联网 发布:别克车钥匙淘宝图片 编辑:程序博客网 时间:2024/06/05 00:32
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
3aaa*abba.*abbaab
Sample Output
yesyesno
思路
题目跟正则表达式类似,给了一个原串,给了一个匹配串,问匹配串可不可以匹配原串.在题目中给的匹配字符有两个:
- ‘.’可以匹配任意字符
- ‘*’可以匹配它之前的一个字符任意次(可以为0)
设有两个串,分别是a和b串,分别代表原串和匹配串,令dp[i][j]代表b串中[1~i]和a串中[1~j]的字符是否匹配,则有以下状态转移关系:
1. 当
2. 当
代码
#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 1000007#define ll long longusing namespace std;const int N=2500+10;char a[N],b[N];bool dp[N][N];int main(){ int t; scanf("%d",&t); getchar(); while(t--) { mem(dp,0); a[0]=b[0]='0'; gets(a+1); gets(b+1); int lena=strlen(a)-1; int lenb=strlen(b)-1; dp[0][0]=true; for(int i=1;i<=lenb;i++) { if(i==2&&b[i]=='*') dp[2][0]=true; for(int j=1;j<=lena;j++) { if(b[i]=='.'||b[i]==a[j]) dp[i][j]=dp[i-1][j-1]; else if(b[i]=='*') { dp[i][j]=dp[i-2][j]|dp[i-1][j]; if((dp[i-1][j-1]||dp[i][j-1])&&a[j-1]==a[j])//匹配连续的* dp[i][j]=true; } } } if(dp[lenb][lena])puts("yes"); else puts("no"); } return 0;}
- HDU6170 Two strings(dp,2017 HDU多校联赛 第9场)
- HDU6170 Two strings dp 多校联赛第9场
- hdu-6170. Two strings (2017多校联赛 dp)
- HDU6170-Two strings 多校9 dp
- 2017多校第9场 HDU 6170 Two strings DP
- HDU6181 Two Paths(次短路,路径记录,spfa,2017 HDU多校联赛 第10场)
- Two strings hdu6170 dp递推
- 2017多校联合第9场1010 Two String/hdu 6170(正则表达式/dp)
- HDU6170-Two strings
- HDU6170-Two strings
- hdu 5791 two 多校联赛第五场
- HDU6165 FFF at Valentine(深搜dfs,2017 HDU多校联赛 第9场)
- HDU6168 Numbers(模拟,2017 HDU多校联赛 第9场)
- HDU6168 Numbers(模拟,2017 HDU多校联赛 第9场)
- HDU6165 FFF at Valentine(深搜dfs,2017 HDU多校联赛 第9场)
- hdu6170-多看几遍之DP&递推&字符串-Two strings
- 2017多校第9场-Two strings(DP)
- hdu 4627 (多校联赛第3场1008)
- Intellij Idea(Mac)调试功能总结
- 做了这么多年的技术,你有写过博客吗?
- Python中的IO编程
- 【整理】web前端相关知识文档手册整理
- 关于kafka的学习
- HDU6170 Two strings(dp,2017 HDU多校联赛 第9场)
- snaker mybatis 配置
- 刷题——Wireless Network POJ
- line-height 和 max-height 的配合作用
- 递推求逆元,求组合数
- Oracle中rank() over, dense_rank(), row_number() 的区别
- 最常见的二维码功能
- 随机采样池化--S3Pool: Pooling with Stochastic Spatial Sampling
- EF Core的安装、EF Core与数据库结合