HDU6170 Two strings(dp,2017 HDU多校联赛 第9场)
2017-08-23 15:06
495 查看
Description
Giving two strings and you should judge if they are matched. The firststring 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 testcases. (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
思路
题目跟正则表达式类似,给了一个原串,给了一个匹配串,问匹配串可不可以匹配原串.在题目中给的匹配字符有两个:‘.’可以匹配任意字符
‘*’可以匹配它之前的一个字符任意次(可以为0)
设有两个串,分别是a和b串,分别代表原串和匹配串,令dp[i][j]代表b串中[1~i]和a串中[1~j]的字符是否匹配,则有以下状态转移关系:
1. 当b[i]==′.′或者 b[i]==a[j]时,此时dp[i][j]=dp[i−1][j−1]
2. 当b[i]==′∗′时,则匹配一个或者不匹配都合法,那么就由dp[i−2][j]或dp[i−1][j]转移而来,因为′∗′可以匹配任意数量的字符,所以还要判断假如 a[j−1]与b[i−1] 已成功匹配,并且a[j−1]==a[j],显然当前的 * 可以继续匹配这一个字符,因此 dp[i][j]=true ,假如 a[j−1]与b[i] 已成功匹配(当前 * 已成功匹配若干位),且 a[j−1]==a[j] ,则可以继续匹配这一个字符,因此dp[i][j]=true 。
代码
#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 long using namespace std; const int N=2500+10; char a ,b ; bool dp ; 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; }
相关文章推荐
- 【DP】HDU 6170/2017多校9 1010 Two strings
- 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场)
- HDU 2017多校联赛-1011 KazaQ's Socks
- 2017 杭电多校联赛第二场 1001 Is Derek lying? HDU 6045
- HDU6098 Inversion(RMQ,2017 HDU多校联赛 第6场)
- HDU - 6170 Two strings (多校,DP,字符串匹配)
- 2017 杭电多校联赛第二场 1003 Maximum Sequence(单调队列)HDU 6047
- HDU 2017多校联赛-Maximum Sequence
- HDU 2017 多校联赛2 1011 Regular polygon
- HDU6103 Kirinriki(尺取法,2017 HDU多校联赛 第6场)
- 2017多校9 Two string hdu 6170 dp
- hdu 6035:Colorful Tree (2017 多校第一场 1003) 【树形dp】
- HDU 2017 多校联赛-1001 Add More Zero
- HDU6092---Rikka with Subset(2017多校联赛:逆向dp)
- HDU6075 Questionnaire(思路,2017 HDU多校联赛 第4场)
- 2017 杭电多校联赛 1002 Balala Power!(数字替换字母)HDU 6034
- HDU 6170 && 2017 多校训练:Two strings(DP)