寻找涉及数学的平衡括号

时间:2014-09-17 19:10:44

标签: java math discrete-mathematics catalan

过去几个小时我试图解决这个问题,我只是不明白。我知道必须有一种数学计算来计算这个,但我不知道如何精确计算它。我知道这段代码没有意义,因为我完全迷失了,我会很感激任何提示或帮助,以帮助我更接近解决方案。

我问我的教授,他告诉我一个暗示,它类似于使用字母表的排列/组合,如26 ^ 3的3种不同的组合,但这对我没什么帮助。

我所知道的:

  1. 字符串中给出的输入有796个字符,我必须找到796个字符可以采用平衡括号形式的所有可能方式。

  2. 由于它必须以'('和以')结尾',每种情况必须有2个括号。所以它可以是'()()(xc)(cvs)'。因此,这意味着数学计算必须涉及每个字符2 *(某事物),因为它必须是平衡的。

  3. 我需要使用余数(%)运算符递归查找每个案例,但如果我在char中取int而不是if怎么办?

    < / LI>

    我不知道的事情:

    1. 我如何分析每个案例?如果没有简单的公式来计算输入,那么需要花费很长时间或大量代码吗?

    2. 我是否需要大量import java.util.*; public class findBrackets { public static void main(String[] args) { String n; int answer = 0; Scanner input = new Scanner(System.in); System.out.println("Input String"); n = input.nextLine(); // probably wrong because a string can start as x(d))c(()... for(int i = 0; i < n; i++) { if(n[i] != '(' || n[i] != ')' || n[i] != null || n[i] != " ") { answer = 2 * (Integer.parseInt(n[i]); // how can i calculate if its a char // i have to use mod % operator somewhere but I don't know where? } } System.out.println("f(796) mod 997 = " + answer); } } 语句或递归?

    3. 问题:

      设Σ= {),(}。设L⊆Σ*是正确平衡括号的字符串集合。例如,(())()在L和(())中(不在L中)形式上,L的递归定义如下。

      ε∈L

      当且仅当x的形式为(y)z时,字符串x≠ε在L中,其中y和z在L中。

      n是0到999之间的特定3位数字。

      计算f(n)mod 997

      您可能会发现一些有用的事实:如果n1,n2是N(自然数)的成员,那么,

      (n1 x n2)mod 997和 (n1 + n2)mod 997

      n = 796(这是我特有的,在这种情况下这将是给定的输入)

      所以我必须“计算f(796)mod 997 =?”使用程序。在这种情况下,我将简单地使用java来解决这个问题。

      代码:

      {{1}}

3 个答案:

答案 0 :(得分:2)

您可能会发现以下事实有用:n对平衡括号的字符串数由第n Catalan number给出,其确切值为

  

(2N)! /(n!(n + 1)!)

你应该能够通过使用关于产品和总和如何分配超过模数的提示直接计算这个值mod 997。

希望这有帮助!

答案 1 :(得分:0)

我仍然不太确定你问的是什么,但是可以使用以下方法验证括号是否是有效的放置。我使用类似的文章来翻阅百页论文,以确保过去所有括号都被正确关闭。

public static boolean isValid(String s) {
    int openParens = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == '(') {
            // we found an open paren
            openParens++;
        } else if (s.charAt(i) == ')') {
            // we can close a paren
            openParens--;
        }
        if (openParens < 0) {
            // we closed a paren but there was nothing to close!
            return false;
        }
    }
    if (openParens > 0) {
        // we didn't close all parens!
        return false;
    }
    // we did!
    return true;
}

答案 2 :(得分:0)

你需要实现这个:

public static void main (String[]args) {
    String str = "((1+2)*(3+4))-5";
    if(isValid(str)){
        expandString(str);
    }
}

public static boolean isValid(String s) {
    int totalParenthesis = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == '(') {
            totalParenthesis++;
        } else if (s.charAt(i) == ')') {
            totalParenthesis--;
        }
        if (totalParenthesis < 0) {
            return false;
        }
    }
    if (totalParenthesis != 0) {
        return false;
    }
    return true;
}

private static void expandString(String str) {
    System.out.println("Called with : "+str);
    if(!(str.contains("("))){
        evalueMyExpresstion(str);
        return;
    }
    String copyString=str;
    int count=-1,positionOfOpen=0,positionOfClose=0;
    for(Character character : str.toCharArray()) {
        count++;
        if(count==str.toCharArray().length){
            evalueMyExpresstion(str);
            return;
        } else if(character.equals('(')) {
            positionOfOpen=count+1;
        } else if(character.equals(')')) {
            positionOfClose=count;
            copyString = str.substring(0, positionOfOpen - 1) + evalueMyExpresstion(
                        str.substring(positionOfOpen, positionOfClose)) + str.substring(positionOfClose + 1);
            System.out.println("Call again with : "+copyString);
            expandString(copyString);
            return;
        }
    }
}

private static String evalueMyExpresstion(String str) {
    System.out.println("operation : "+str);
    String[] operation;
    int returnVal =0;
    if(str.contains("+")){
        operation = str.split("\\+");
        returnVal=Integer.parseInt(operation[0])+ Integer.parseInt(operation[1]);
        System.out.println("+ val : "+returnVal);
        return Integer.toString(returnVal);
    } else if (str.contains("*")){
        operation = str.split("\\*");
        returnVal=Integer.parseInt(operation[0])* Integer.parseInt(operation[1]);
        System.out.println("* val : "+returnVal);
        return Integer.toString(returnVal);
    } else if (str.contains("-")){
        operation = str.split("\\-");
        returnVal=Integer.parseInt(operation[0])- Integer.parseInt(operation[1]);
        System.out.println("- val : "+returnVal);
        return Integer.toString(returnVal);
    }
    System.out.println(str);
    return Integer.toString(returnVal);
}

输出如下:

Called with : ((1+2)*(3+4))-5
operation : 1+2
+ val : 3
Call again with : (3*(3+4))-5
Called with : (3*(3+4))-5
operation : 3+4
+ val : 7
Call again with : (3*7)-5
Called with : (3*7)-5
operation : 3*7
* val : 21
Call again with : 21-5
Called with : 21-5
operation : 21-5
- val : 16