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;
char a[N], b[N];
int la, lb;
bool dp[N][N];
int main()
{
    scanf("%d", &t);
    while (t--)
    {
        scanf("%s%s", a+1, b+1);
        la = strlen(a+1);
        lb = strlen(b+1);
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        for (int i = 1; i <= lb; i++)
        {
            if (b[i] == ‘.‘)
            {
                for (int j = 1; j <= la; j++) dp[i][j] = dp[i-1][j-1];
            }
            else if (b[i] == ‘*‘)
            {
                for (int j = 0; j <= la; j++) dp[i][j] = dp[i-2][j];
                for (int j = 0; j <= la; )
                {
                    if (dp[i-1][j])
                    {
                        int k = j;
                        while (a[k] == a[j])
                        {
                            dp[i][k] = 1;
                            k++;
                        }
                        j = k;
                    }
                    else j++;

                }
            }
            else
            {
                for (int j = 1; j <= la; j++)
                    if (dp[i-1][j-1] && a[j] == b[i])
                        dp[i][j] = 1;
            }
        }
        if (dp[lb][la]) puts("yes");
        else puts("no");
    }
}

  

时间: 2024-08-02 05:00:52

HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9的相关文章

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 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&

HDU 6170 Two strings (DP)

题意:给定两个字符串,问你是不是匹配,这不是完全的正则表达式,而且题意有点模糊,'.'能匹配任意字符.'*'能匹配前面一个字符重复0-无数多次,如果是 . *  这样的是先匹配 .,再匹配*. 析:dp[i][j] 表示 第一个串匹配到 i 第二串匹配到 j,是不是能. 如果是a[i] == b[j] 那就是 dp[i-1][j-1] 如果 b[j] == '.' 就是 dp[i-1][j-1] 如果 b[j] == '*' 那么要分情况,一种是 a[i] == a[i-1]  就是 dp[i-

hdu 6170 Two strings

dp[i][j]代表s1的前i个字符和s2的前j个字符匹配的情况,记录此时s2[j-1]匹配的字符.-1代表不匹配,0代表s2[j-1]匹配空字符. #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<iostream> #include<queue> #include<m

HDU 6168 - Numbers | 2017 ZJUT Multi-University Training 9

/* HDU 6168 - Numbers [ 思维 ] | 2017 ZJUT Multi-University Training 9 题意: .... 分析: 全放入multiset 从小到大,慢慢筛 */ #include <bits/stdc++.h> using namespace std; const int N = 125250; int n, s[N]; int a[N], cnt; multiset<int> st; multiset<int>::it

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 4906 Our happy ending(2014 Multi-University Training Contest 4)

题意:构造出n个数 这n个数取值范围0-L,这n个数中存在取一些数之和等于k,则这样称为一种方法.给定n,k,L,求方案数. 思路:装压 每位 第1为表示这种方案能不能构成1(1表示能0表示不能)  第2为表示能不能构成2 ...  这样用d[1<<n] 的DP  像背包那样背 n次就可以 最后状态中第k位为1的就可以加上方法数. #include<cstring> #include<cstdio> #include<cmath> #include <

HDU 4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

题意:给定n*m个格子,每个格子能填0-k 的整数.然后给出每列之和和每行之和,问有没有解,有的话是不是唯一解,是唯一解输出方案. 思路:网络流,一共 n+m+2个点   源点 到行连流量为 所给的 当前行之和.    每行 连到每一列 一条流量为  k的边,每列到汇点连 列和.如果流量等于总和则有解,反之无解(如果列总和不等于行总和也无解).  判断方案是否唯一 找残留网络是否存在长度大于2的环即可,有环说明不唯一. #include<cstdio> #include<cstring&

HDU 6170 dp

Two strings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 485    Accepted Submission(s): 178 Problem Description Giving two strings and you should judge if they are matched.The first string co