小H和密码

好难的dp…
好蠢的我…
勉强苟出来了,但是还不太清楚..这题很重要啊,要赶紧来补..
对于一个dp数组,每个都要遍历到,每个dp状态,如果可以列举出他所有的可以来源,并在来源处转移,可以转移到,(转移方程可以写出)没有遗漏,就是可行的.
dp[i][j],是否可以用前i个表达前j个,要不他自己是空的,要不是自己对应最后一个.

#include <iostream>
using namespace std;
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
typedef long long LL;
const int MAXN = 3e2+17;
int dp[MAXN][MAXN],have[MAXN][27];
int main(int argc ,char const *argv[])
{
    #ifdef noob
    freopen("Input.txt","r",stdin);freopen("Output.txt","w",stdout);
    #endif      
    int n,m,q;
    cin>>n>>m>>q;
    for (int i = 0; i < n; ++i)
    {
        string str;
        cin>>str;
        for (int j = 0; j < str.length(); ++j)
        {
            if(str[j]=='#') have[i+1][26]++;
            else have[i+1][str[j]-'a']++;
        }
    }
    while(q--)
    {
        string str;
        cin>>str;
        int l = str.length();
        if(l>n)
        {
            cout<<"NO"<<endl;
            continue;
        }
        for (int i = 0; i < 317; ++i)
            for (int j = 0; j < 317; ++j)
                dp[i][j] = 0;
        dp[0][0] = 1;
        for (int i = 0; i <= n-1; ++i)
        {
            for (int j = 0; j <= n-1; ++j)
            {
                if(dp[i][j])
                {
                    if(have[j+1][26]) dp[i][j+1] = 1;
                    if(have[j+1][str[i]-'a']) dp[i+1][j+1] = 1;
                }
            }
        }
        if(dp[l][n]) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;   
}

猜你喜欢

转载自blog.csdn.net/m0_37802215/article/details/79984580