将字符串括起来以使表达式具有给定值

时间:2011-12-28 06:04:40

标签: algorithm dynamic-programming parentheses boolean-expression

以下问题来自Vazirani等人关于动态规划的章节。人


[6.6]让我们在三个符号a上定义乘法运算(×); b; c根据下表:

Multiplication table

因此,a×a = b,a×b = b等。

找到一种有效的算法,检查这些符号的字符串,比如说bbbbac,然后决定 是否有可能以这样的方式括起字符串的值 结果表达是一个。例如,在输入bbbbac时,您的算法应返回yes,因为 ((b(bb))(ba))c = a。


这是我的方法:将其映射到计算布尔括号的数量的问题,如 here 。在该问题中,您将获得表单

的布尔表达式

T F T xor T

并且您需要找到括号的方法的数量,以便它的计算结果为true。

我们可以将 xor 视为符合某些规则的运算符(T xor F = T等)并对采用值T或F的操作数进行操作。对于我们的原始问题,我们可以将a,b,c视为具有乘法(x)的操作数,如给定表所定义的那样提供规则。

上述方法是否有意义或是否有更简单的方法?

2 个答案:

答案 0 :(得分:0)

是的,您的方法应该类似于您提到的问题。一般来说,如果有 n 符号(而不是你在这个问题中提到的3个符号,或者你给出链接的问题中的2个符号),你应该做什么像这样的东西 -

#include <stdio.h>
#include <string.h>

#define MAXL 500
#define MAXN 100

int     isPossible[MAXL][MAXL][MAXN];
int     matrix[MAXN][MAXN]; //multiplication table
char    str[MAXN+1];
int     L;

int go(int start, int end, int need) {
    if(start > end) return 0;
    if(isPossible[start][end][need] != -1) return isPossible[start][end][need];

    int i,x,y;
    for(i = start; i < end; i++) {
        for(x = 0; x < MAXN; x++) {//you can optimize these x & y loops by pre-determining which combinations can give you 'need'
            for(y = 0; y < MAXN; y++) if(matrix[x][y] == need) {
                if(go(start, i, x)==1 && go(i+1, end, y)==1 ) {
                    isPossible[start][end][need] = 1;
                    return 1;
                }
            }
        }
    }
    return 0;
}

int main() {
    while(scanf(" %s",str)==1) {
        L = strlen(str);
        memset(isPossible, -1, sizeof(isPossible));
        go(0, L-1, 'a');
    }
    return 0;
}

请注意,这不是经过测试的完整源代码。

答案 1 :(得分:0)

我们可以通过动态编程来解决这个问题pseudo-Algorithm可以在这里找到。

/**
* Parenthesizing a string so that expression takes a given value
*/
import java.util.*;
class Solution
{
static boolean func(int[][] matrix, int[] str, int n, int symbol)
{
    Set<Integer>[][] T = new Set[n][n];

    // Assign the value
    for(int i=0; i<n; i++)
    {
        T[i][i] = new HashSet<Integer>();
        T[i][i].add(str[i]);
    }


     for(int gap = 1; gap<n; gap++)
     {
         for( int i = 0, j = gap; j<n; i++, j++)
         {       
             T[i][j] =  new HashSet<Integer>();

             for(int k=i; k < i+gap; k++)
             {
                 Iterator<Integer> outer = T[i][k].iterator();
                 while(outer.hasNext())
                 {
                     int elementOuter = outer.next();
                     Iterator<Integer> inner = T[k+1][j].iterator();
                     while(inner.hasNext())
                     {
                         int elementInner = inner.next();
                         int val = matrix[elementOuter][elementInner];
                         T[i][j].add(val);
                     }
                 }
             }
         }

     }
     if(T[0][n-1].contains(symbol))
         return true;
     return false;
}


public static void main(String args[] ) throws Exception 
{
    int[] stringNew = {1, 1, 1, 1, 0}; // for String "bbbbac"
    int element = 3;
    /**
     * Here a -> 0       
     *      b -> 1
     *      c -> 2
     *      
     *      Table                  Equivalent Table
     *      * a b c         \      * 0 1 2
     *      a b b a    ------\     0 1 1 0
     *      b c b a    ------/     1 2 1 0
     *      c a c c         /      2 0 2 2
     */
    int     matrix[][] = {{1, 1, 0},{2, 1, 0},{0, 2, 2}}; //multiplication table

    System.out.println(func(matrix, stringNew, stringNew.length, 0));
 }
}