zoukankan      html  css  js  c++  java
  • hdu 6170 Two strings(dp)

    多校比赛时,我被题意杀了.这里的*指的是可以重复前面字符任意多次,甚至可以删去自己和前面的字符,即重复0次.考试的时候不知道,结果咸鱼了.

    首先dp思维很简单f[i][j]表示s前i个和t前j个能否匹配,三种转移

    1.直接匹配,由f[i-1][j-1]直接转移过来

    2.*替换字符,由f[i-1][j]转移过来

    3.删除*或*和前面的字符,由f[i-1][j]或f[i-2][j]转移过来

    初始化时注意一个细节

    s=空串

    t=a*

    这两个实际是可以匹配的.

    然后这题就没什么了

    贴代码:

    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int LEN=2510;
    char s[LEN],t[LEN];
    bool f[LEN][LEN];
    int ttt,lens,lent;
    inline bool check(const int &i,const int &j){
        return s[i]==t[j]||t[j]=='.';
    }
    void prework(){
        scanf("%s%s",s+1,t+1);
        lens=strlen(s+1); 
        lent=strlen(t+1);
        memset(f,0,sizeof(f));
    }
    int main(){
        scanf("%d",&ttt);
        while (ttt--){
            prework();
            f[0][0]=1;
            if (t[2]=='*') f[0][2]=1;
            for (int i=1; i<=lens; i++)
            for (int j=1; j<=lent; j++){
                if (check(i,j)) f[i][j]|=f[i-1][j-1];
                if (t[j]=='*'&&s[i]==s[i-1]) f[i][j]|=f[i-1][j];
                if (t[j]=='*') f[i][j]|=f[i][j-1]|f[i][j-2];
            }
            puts(f[lens][lent]?"yes":"no");
        }
    }
  • 相关阅读:
    day25:接口类和抽象类
    vue1
    How the weather influences your mood?
    机器学习实验方法与原理
    How human activities damage the environment
    Slow food
    Brief Introduction to Esports
    Massive open online course (MOOC)
    Online learning in higher education
    Tensorflow Dataset API
  • 原文地址:https://www.cnblogs.com/Yuhuger/p/7413050.html
Copyright © 2011-2022 走看看