zoukankan      html  css  js  c++  java
  • 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-1][j-1] | dp[i-1][j] 

    另一种无所谓 就是  dp[i][j-1] | dp[i][j-2]

    提供几个数据:

    ababb
    ababb*

    yes

    b
    ba*

    yes

    aab
    c*a*b

    yes

    b
    baa*

    no

    abc
    .*

    no

    abbdaaabccc
    .*...*.b*.*

    no

    aac
    c*aac

    yes

    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    aa*aa.*aaa

    yes

    aaaaaaaaaaaaaaaaaaaaaaaa

    a*aa*a*a*a*a*aa*a*a*a*a*a*a*a*

    yes

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define FOR(x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 2500 + 10;
    const LL mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
        return r > 0 && r <= n && c > 0 && c <= m;
    }
    
    bool dp[maxn][maxn];
    char a[maxn], b[maxn];
    
    int main(){
      freopen("1010.in", "r", stdin);
      freopen("out.txt", "w", stdout);
      int T;  cin >> T;
      while(T--){
        scanf("%s", a+1);
        scanf("%s", b+1);
        ms(dp, 0);
        dp[0][0] = true;
        n = strlen(a+1);
        m = strlen(b+1);
        for(int i = 0; i <= n; ++i){
          for(int j = 1; j <= m; ++j){
            if(a[i] == b[j])  dp[i][j] |= dp[i-1][j-1];
            else if(b[j] == '.')  dp[i][j] |= dp[i-1][j-1];
            else if(b[j] == '*' && a[i] == a[i-1])  dp[i][j] |= dp[i-1][j-1] | dp[i-1][j];
            if(b[j] == '*')  dp[i][j] |= dp[i][j-1] | dp[i][j-2];
          }
        }
        puts(dp[n][m] ? "yes" : "no");
      }
      return 0;
    }
    

      

  • 相关阅读:
    JavaScript技巧
    函数
    windows实现应用双开
    vue组件中name属性有啥作用
    文本超出长度后多余部分显示省略号
    el-tree控件动态获取数据赋值给treeData渲染问题:render-after-expand属性
    elementUI弹框dialog的打开和关闭
    自然语言处理工具之gensim / 预训练模型 word2vec doc2vec
    Linux 根目录空间不足解决方法
    文本挖掘预处理之分词 / 向量化 / TF-IDF / Hash trick 附代码 Demo
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7425072.html
Copyright © 2011-2022 走看看