BZOJ 1055 - 区间dp

Magic Door

题目大意:

WING四个字符都分别可以用若干个两个字符(只可能是WING)表示,给一个只含有WING四种字符的字符串,问给定的字符串可能最初是由WING哪些字符演变而来,按照WING输出。

题目分析

区间dp: dp[i][j][t]表示达到i~j 是否能缩成t(W对应0, I对应1, N对应2, G对应3)。
假设t可以演变为ab, cd(举例), 则如果dp[i][k][ab] && dp[k + 1][j][cd],那么dp[i][j][t]为真。即
\[dp[i][j][t] = (dp[i][k][ab] \&\& dp[k + 1][j][cd])\] 注意一旦可以为真就退出,可以节约大量时间。
边界情况: 这是我自己的一些做法,首先将仅有的16种双字符用1~16表示,每个值下记录可以由WING哪些演变而来。初始化dp数组,扫遍一边字符串,举例如果i~i + 1的字符是ab, 而ab又可以由W(0)和I(1)转移来,那么dp[i][i + 1][0 and 1] = true。其次如果第i个字符是W(0), 那么dp[i][i][0] = true。
然后跑一遍记忆花搜索即可。

code

#include<bits/stdc++.h>
using namespace std;

#define W 0
#define I 1
#define N 2
#define G 3
const int Maxn = 300, OO = 0x3f3f3f3f;
int num[4], len;
char str[4][100][5], s[Maxn];
int dp[Maxn][Maxn][4];
vector<int> hash[20];

inline int getHashVal(char s1, char s2){
    if(s1 == 'W' && s2 == 'W') return 1;
    if(s1 == 'W' && s2 == 'I') return 2;
    if(s1 == 'W' && s2 == 'N') return 3;
    if(s1 == 'W' && s2 == 'G') return 4;
    if(s1 == 'I' && s2 == 'W') return 5;
    if(s1 == 'I' && s2 == 'I') return 6;
    if(s1 == 'I' && s2 == 'N') return 7;
    if(s1 == 'I' && s2 == 'G') return 8;
    if(s1 == 'N' && s2 == 'W') return 9;
    if(s1 == 'N' && s2 == 'I') return 10;
    if(s1 == 'N' && s2 == 'N') return 11;
    if(s1 == 'N' && s2 == 'G') return 12;
    if(s1 == 'G' && s2 == 'W') return 13;
    if(s1 == 'G' && s2 == 'I') return 14;
    if(s1 == 'G' && s2 == 'N') return 15;
    if(s1 == 'G' && s2 == 'G') return 16;
}

inline void get(char p, int &t){
    switch(p){
        case 'W': t = W; break;
        case 'I': t = I; break;
        case 'N': t = N; break;
        case 'G': t = G; break;
    }
}

inline void init(){
    for(int i = 1; i < len; i++){
        int h = getHashVal(s[i], s[i + 1]);
        for(int j = 0; j < hash[h].size(); j++)
            dp[i][i + 1][hash[h][j]] = 1;
    }
    for(int i = 1; i <= len; i++){
        int t; get(s[i], t);
        dp[i][i][t] = 1;
    }
}



inline int DP(int l, int r, int t){
    if(dp[l][r][t] != -1) return dp[l][r][t];
    int flag = 0;
    for(int k = l; k < r; k++){
        for(int i = 1; i <= num[t]; i++){
            char s1 = str[t][i][1], s2 = str[t][i][2];
            int t1, t2;
            get(s1, t1), get(s2, t2);
            if(DP(l, k, t1) == 1 && DP(k + 1, r, t2) == 1){
                flag = 1;
                return dp[l][r][t] = flag;
            }
        }
    }
    return dp[l][r][t] = flag;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    for(int i = W; i <= G; i++) cin >> num[i];
    for(int i = W; i <= G; i++)
        for(int j = 1; j <= num[i]; j++){
            cin >> str[i][j] + 1;
            hash[getHashVal(str[i][j][1], str[i][j][2])].push_back(i);
        }
    cin >> s + 1; len = strlen(s + 1);
    memset(dp, -1, sizeof dp);
    init();
    int cnt = 0;
    for(int i = W; i <= G; i++)
        if(DP(1, len, i) == 1){
            cnt++;
            switch(i){
                case 0: cout << 'W'; break;
                case 1: cout << 'I'; break;
                case 2: cout << 'N'; break;
                case 3: cout << 'G'; break;
            }
        }
    if(!cnt) cout << "The name is wrong!" << endl;
    return 0;
}

转载于:https://www.cnblogs.com/CzYoL/p/7636255.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值