HDU 6170Two strings

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

正则表达式的问题。

题解说的是用动态规矩。

str表示主串,str1表示模拟串,可以假设dp[i][j]表示str1[1,i]和str[1,j]是否匹配。

显然dp[0][0] = true.

str1[i] == . 或者str1[i] == str[j]时,dp[i][j] 看状态dp[i-1][j-1]

str1[i] == ‘*‘时,dp[i][j] == dp[i-1][j] | dp[i-2][j],而当(dp[i-1][j-1] || dp[i][j-1]) && str[j-1] == str[j] 时,dp[i][j]必定为true;

详细参考

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 using namespace std;
 6 const int N = 2510;
 7 char str[N], str1[N];
 8 bool dp[N][N];
 9 int main() {
10     int t;
11     scanf("%d", &t);
12     while(t--) {
13         memset(dp, false, sizeof(dp));
14         scanf("%s %s",str+1, str1+1);
15         int len = strlen(str+1), len1 = strlen(str1+1);
16         dp[0][0] = true;
17         for(int i = 1; i <= len1; i ++) {
18             if(i == 2 && str1[i] == ‘*‘) dp[i][0] = true;
19             for(int j = 1; j <= len; j ++) {
20                 if(str1[i] == ‘.‘ || str1[i] == str[j])
21                     dp[i][j] = dp[i-1][j-1];
22                 else if(str1[i] == ‘*‘) {
23                     dp[i][j] = dp[i-2][j] | dp[i-1][j];
24                     if((dp[i-1][j-1] || dp[i][j-1]) && str[j-1] == str[j])
25                         dp[i][j] = true;
26                 }
27             }
28         }
29         printf("%s\n",dp[len1][len]?"yes":"no");
30     }
31     return 0;
32 }
时间: 2024-10-18 06:32:13

HDU 6170Two strings的相关文章

HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9

/* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问两串是否匹配 分析: dp[i][j] 代表B[i] 到 A[j]全部匹配 然后根据三种匹配类型分类讨论,可以从i推到i+1 复杂度O(n^2) */ #include <bits/stdc++.h> using namespace std; const int N = 2505; int t;

矩阵十题【五】 VOJ1049 HDU 2371 Decode the Strings

题目链接:https://vijos.org/p/1049 题目大意:顺次给出m个置换,反复使用这m个置换对初始序列进行操作,问k次置换后的序列.m<=10, k<2^31. 首先将这m个置换"合并"起来(算出这m个置换的乘积),然后接下来我们需要执行这个置换k/m次(取整,若有余数则剩下几步模拟即可).注意任意一个置换都可以表示成矩阵的形式.例如,将1 2 3 4置换为3 1 2 4,相当于下面的矩阵乘法: 置换k/m次就相当于在前面乘以k/m个这样的矩阵.我们可以二分计

HDU 2736 Surprising Strings

Surprising Strings Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description The D-pairs of a string of letters are the ordered pairs of letters that are distance D from each other. A string is D-unique if all of its D-

hdu 5229 ZCC loves strings

题意: CC有N个字符串,他正在和Miss G.用这N个字符串玩一个小游戏.ZCC会从这N个串中等概率随机选两个字符串(不可以是同一个).然后,ZCC和Miss G.轮流操作.Miss G.总是先操作的.在每轮中,操作者可以选择操作A或操作B. 操作A:在两个串中选择一个当前非空的串,然后在这个串的末尾删去一个字符. 操作B: 若当前两个串完全相同且非空,则可以使用这个操作.此时两个串都被清空. 不能操作的玩家输掉了这个游戏. ZCC想要知道他输掉游戏的概率是多少(也就是Miss G.获胜的概率

HDU 6170 Two strings 思维 DP

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6170 题目描述: 两个字符串问是否匹配, '.'可以匹配任意字符, '*'可以使前一个数的出现次数成上一个自然数(0, 1, 2, 3........) 解题思路: DP, dp(i, j)表示A串匹配到j位, B串匹配到i位两个串是否匹配, 转移方程再代码里有, 参考Jaihk662的博客, 注释在代码中 代码: #include <iostream> #include <cstdio&

2017多校第9场 HDU 6170 Two strings DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6170 题意:给了2个字符串,其中第2个字符串包含.和*两种特别字符,问第二个字符串能否和第一个匹配. 解法:dp[i][j]代表在第一个串的i位置,第2个串的j位置是否可以匹配,然后按照'*'这个特殊情况讨论转移即可. #include <bits/stdc++.h> using namespace std; const int maxn = 3005; bool dp[maxn][maxn];

HDU 1075 What Are You Talking About (strings)

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 15966    Accepted Submission(s): 5177 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

hdu 5229 ZCC loves strings(Bestcoder Round #41)

ZCC loves strings                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Total Submission(s): 286    Accepted Submission(s): 108 Problem Description ZCC has got N st

hdu 2406 Power Strings KMP

Power Strings                                                               Time Limit:3000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Description Given two strings a and b we define a*b to be their concatenation. For example, if a =