洛谷P4290 [HAOI2008]玩具取名

链接

点击跳转

题解

f i j c f_{ijc} 表示区间 [ i , j ] [i,j] 能否缩成一个字母 c c

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 210
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
char s[maxn];
int ok[4][4][4], tb[300], f[maxn][maxn][4];
int main()
{
    int i, j, k, l, a, b, c, d, n, lc, rc;
    tb['W']=0;
    tb['I']=1;
    tb['N']=2;
    tb['G']=3;
    cin>>a>>b>>c>>d;
    rep(i,1,a)
    {
        cin>>s;
        ok[ tb[s[0]] ][ tb[s[1]] ][ 0 ]=1;
    }
    rep(i,1,b)
    {
        cin>>s;
        ok[ tb[s[0]] ][ tb[s[1]] ][ 1 ]=1;
    }
    rep(i,1,c)
    {
        cin>>s;
        ok[ tb[s[0]] ][ tb[s[1]] ][ 2 ]=1;
    }
    rep(i,1,d)
    {
        cin>>s;
        ok[ tb[s[0]] ][ tb[s[1]] ][ 3 ]=1;
    }
    cin>>s+1;
    n=strlen(s+1);
    rep(i,1,n)s[i]=tb[s[i]];
    rep(i,1,n)f[i][i][s[i]]=1;
    rep(l,2,n)rep(i,1,n-l+1)
    {
        j = i+l-1;
        rep(k,i,j-1)
        {
            rep(lc,0,3)rep(rc,0,3)
            {
                rep(c,0,3)if(ok[lc][rc][c])f[i][j][c] |= f[i][k][lc]&f[k+1][j][rc];
            }
        }
    }
    bool flag = f[1][n][0]|f[1][n][1]|f[1][n][2]|f[1][n][3];
    if(!flag)
    {
        cout << "The name is wrong!";
    }
    else
    {
        if(f[1][n][0])cout<<"W";
        if(f[1][n][1])cout<<"I";
        if(f[1][n][2])cout<<"N";
        if(f[1][n][3])cout<<"G";
    }    
    return 0;
}
发布了948 篇原创文章 · 获赞 77 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/FSAHFGSADHSAKNDAS/article/details/105054163