提问,高手进来回答!

2024-12-12 18:31:50
推荐回答(3个)
回答1:

一楼:你的算法漏洞太大了吧?“1+[2+(3+{4+5])}”照你的算法是匹配的吧?
二楼:的确应该用递归,但是你的算法只能处理三层括号啊?第四层怎么办啊:1+(2+{3+[4+(5+6)]})

代码(Java):

import java.util.HashMap;
import java.util.Map;

public class BracesMatcher{
private static Brace square = new Brace('[',']');
private static Brace curly = new Brace('{','}');
private static Brace circle = new Brace('(',')');
private static Map braces = new HashMap();

static {
curly.setOutter(circle);
square.setOutter(curly);
circle.setOutter(square);

braces.put(square.left,square); // Create the relationship
braces.put(circle.left,circle);
braces.put(curly.left,curly);
}

public static void main(String[] args) {
BracesMatcher matcher = new BracesMatcher();
boolean result = matcher.isMatch("{[()]}",null);
System.out.println(result);
}

// Like pattern "{[]()}"
private boolean matchByPatternA(String str,Brace outter) {
System.out.println("Matching \"" + str + "\" using pattern A ...");

int start = getIndexOfFirstLeft(str);
int end = getIndexOfFirstRight(str);

if (start == -1 && end == -1) // No braces
return true;

if ((start == -1 ^ end == -1) || end < start) // Braces not in pair
return false;

char left = str.charAt(start);
char right = str.charAt(end);
Brace brace = braces.get(left);
if (outter != null
&& (!brace.matchRight(right) || !brace.outter.equals(outter)))
// Left and right braces not matched, or level is invalid
return false;

String leftPart = str.substring(start + 1,end); // []
String rightPart = str.substring(end + 1); // ()

return isMatch(leftPart,brace) && isMatch(rightPart,brace);
}

// Like pattern "{[()]}"
private boolean matchByPatternB(String str,Brace outter) {
System.out.println("Matching \"" + str + "\" using pattern B ...");

int start = getIndexOfFirstLeft(str);
int end = getIndexOfLastRight(str);
if (start == -1 && end == -1)
return true;

if ((start == -1 ^ end == -1) || end < start)
return false;

char left = str.charAt(start);
char right = str.charAt(end);
Brace brace = braces.get(left);
if (outter != null
&& (!brace.matchRight(right) || !brace.outter.equals(outter)))
return false;

String sub = str.substring(start + 1,end); // [()]

return isMatch(sub,brace);
}

private int getIndexOfFirstLeft(String str) {
int index = -1;

for (char left:braces.keySet()) {
int i = str.indexOf(left);
index = ((i < index || index == -1) && i != -1) ? i : index;
}

return index;
}

private int getIndexOfFirstRight(String str) {
int index = -1;

for (char left:braces.keySet()) {
int i = str.indexOf(braces.get(left).right);
index = ((i < index || index == -1) && i != -1) ? i : index;
}

return index;
}

private int getIndexOfLastRight(String str) {
int index = -1;

for (char left:braces.keySet()) {
int i = str.lastIndexOf(braces.get(left).right);
index = (i > index) ? i : index;
}

return index;
}

public boolean isMatch(String str,Brace outter) {
boolean result = matchByPatternA(str,outter)
|| matchByPatternB(str,outter);
System.out.println("Matching \"" + str + "\" ... " + result);
return result;
}

private static class Brace{
private char left;
private char right;
private Brace outter;

public Brace(char left,char right){
this.left = left;
this.right = right;
}

public Brace getOutter() {
return this.outter;
}

public void setOutter(Brace outter) {
this.outter = outter;
}

public char getRight() {
return right;
}

public char getLeft() {
return left;
}

public boolean matchRight(char right) {
return right == this.right;
}

public boolean equals(Object o) {
return (o != null && o.getClass().equals(this.getClass()) && ((Brace)o)
.getLeft() == this.left);
}
}
}

回答2:

楼上把C实现的代码都直接给出来了。
楼主没太说清楚括号间是否也要求匹配?即大括号应在中括号之外,中括号应在小括号之外?
设计一个算法思想如下:
开始
读入字符串
对每一个读入的字符:(While或者For循环)
判断是否是{或}
是 不是
大括号标志位+1或-1 next
递归调用此程序,判断是否为[或]
是 不是
中括号标志位+1或-1 next
递归调用此程序,判断是否为(或)
是 不是
小括号标志位+1或-1 next
循环结束
检查标志位,输出信息。

应该写成递归调用,即高级括号内的低级括号不匹配即要报错。
即能够检测出这种情况:
{[(1+1)+1(]+[)1+(1+1)]}
只要第一个中括号里的小括号不匹配就要跳出递归。
实现上可以定义int main,并在判断本层括号前检查低一层返回的值,如不匹配即给出提示。

回答3:

#include
#include

using namespace std;

int main()//假定判断“{}”
{
int ans=0;
string expression;
//......//读入字符串表达式(略)
for(int i=0;i {
if(expression[i]=='{")//可以换成其他相应符号
{
ans++;
}
else if(expression[i]=='}')//可以换成其他相应符号
{
ans--;
}
}
if(ans==0)
cout<<"匹配"< else
cout<<"不匹配"< return 0;
}