HDU 3427

DP:

According to the meaning of problems,if we check n to m, assume x and y are both solvable,then we only should:


(1). check  xy;

(2). check AxA

(3). check AxAyA


if any one of the three is solvable ,  n to m is solvable


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 200;
char str[MAXN], dp[MAXN][MAXN];
bool dfs(int n, int m){
    if(n > m) return true;
    if(n == m) return false;
    if(dp[n][m] == 1) return true;
    if(dp[n][m] == -1) return false;
    for(int i = n+1;i < m-1;i ++){
        if(dfs(n, i) && dfs(i+1, m)){               //check xy;
            dp[n][m] = 1;
            return true;
        }
    }
    if(str[n] == str[m]){
        if(dfs(n+1, m-1)){                          //check AxA
            dp[n][m] = 1;
            return true;;
        }
        for(int i = n+1;i < m;i ++){
            if(str[i] != str[n]) continue;
            if(dfs(n+1, i-1) && dfs(i+1, m-1)){     //check AxAyA
                dp[n][m] = 1;
                return true;
            }
        }
    }
    dp[n][m] = -1;
    return false;
}
int main(){
    memset(str, 0, sizeof(str));
    freopen("in.c", "r", stdin);
    while(~scanf("%s", str)){
        memset(dp, 0, sizeof(dp));
        int len = strlen(str);
        if(dfs(0, len-1)) printf("solvable
");
        else printf("unsolvable
");
    }
    return 0;
}


原文地址:https://www.cnblogs.com/anhuizhiye/p/3933150.html