Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 正确括号的最长子串_C_Algorithm_Stack - Fatal编程技术网

C 正确括号的最长子串

C 正确括号的最长子串,c,algorithm,stack,C,Algorithm,Stack,我遇到了一点问题。我必须计算正确闭括号的最长子字符串,到目前为止,我已经做到了: while (work_stack.size != 0){ //I have a working stack in which I have stored the elements //which in my case are brackets and while I have elements //i pop the first element and see if it's a left

我遇到了一点问题。我必须计算正确闭括号的最长子字符串,到目前为止,我已经做到了:

while (work_stack.size != 0){
    //I have a working stack in which I have stored the elements
    //which in my case are brackets and while I have elements
    //i pop the first element and see if it's a left or right
    a1 = pop(&work_stack.data);
    work_stack.size--;

    if ('{' == ((Pair*)a1->info)->type ||
        '(' == ((Pair*)a1->info)->type ||
        '[' == ((Pair*)a1->info)->type)  {
        //if it's a left bracket, then I add it to the left stack
        //which i'm going to use to compare with the right sided
        //brackets i will encounter.
            stanga++; //i'm incrementing the no of left brackets
        if(ok == 0) //if there wasn't a match, then length is set to 0
            length = 0;
        if (ok == 1 && stanga > 1)
            //if there was a match but then two brackets of left side
            //are encountered, then length = 0
            /*
            Now I figured that here I am wrong. Given the input:
            [][()()])())[][)]
            The right value should be 8, but my code encounters
            two left values and sets the length to 0. I need to 
            find a way to determine if the substring is worth keeping
            */
            length = 0;
        push(&left.data, a1);
        left.size++;
    }
    if ('}' == ((Pair*)a1->info)->type ||
        ')' == ((Pair*)a1->info)->type ||
        ']' == ((Pair*)a1->info)->type){
        //if it's a right bracket and there are elements in the left
        //then i pop the first element fro the left stack and compare
        //it to my current bracket
        if(left.size != 0){
            stanga = 0;
            a2 = pop(&left.data);
            left.size--;

            //opposing is a function that returns 1 if 
            //i encounter something like ( ) or [ ] or { }
            //if the brackets are opposed, i increment the length
            if (oposing(((Pair*)a2->info)->type, ((Pair*)a1->info)->type) == 1){
                length += 2;
                ok = 1;
            }

            //otherwise, it seems that I have run into a stopping 
            //point, so I'm emptying the left stack because those 
            //paranthesis are not of use anymore and I'm saving
            //the maximum length acquired by now
            if (oposing(((Pair*)a2->info)->type, ((Pair*)a1->info)->type) == 0){
                ok = 0;
                while(left.size > 0){
                    a2 = pop(&left.data);
                    left.size--;    
                }
                if(length > max){
                    max = length;
                    length = 0;
                }
            }
            //if i haven't encountered a stopping point, i just 
            //compare the length to my max and save it if it's bigger   
            if (length > max)
                max = length;
        }
        //this line says that if the size of the left stack is 0 and
        //i have encountered a right bracket, then I can't form a 
        //correct substring, so the length is 0
        else length = 0;
    }
}
注意:
((Pair*)a1->info)->type
是我的角色。 非常感谢。 稍后编辑: -我正在为stack和Pair添加结构

typedef struct{
   int id;
   char type;
}Pair;

typedef struct cel{
   void *info;
   struct cel *urm;
}Celula, *TLista, **ALista;

typedef struct{
   TLista data;
   int size;
}stack;
我的堆栈的数据类型是链表,但这并不重要,因为操作是正确的(push和pop)


编辑:在代码中添加了一些新的改进,并在注释中对我正在做的事情进行了新的解释。我发现了这个bug,但没有找到解决方案。

这个问题可以使用堆栈解决。这是我的C++实现,希望你不会觉得理解语法并把它翻译成C.<
int longestValidParentheses(string s) {
    stack <int> Stack;
    int maxLen = 0;
    int lastPos = -1;
    for(int i = 0; i < s.length(); ++i) {
        if(s[i] == '(') {
            Stack.push(i);
        }
        else {
            if(Stack.empty()) {
                lastPos = i;
            }
            else {
                Stack.pop();
                if(Stack.empty()) {
                    maxLen = max(maxLen, i - lastPos);
                } else {
                    maxLen = max(maxLen, i - Stack.top());
                }
            }
        }
    }
    return maxLen;
}
int longestValidParentses(字符串s){
堆叠;
int maxLen=0;
int lastPos=-1;
对于(int i=0;i

对不起,我现在在外面,没有解释,而是代码。如果您需要任何部分的解释,我会进行澄清。现在,您可以使用一些输入和纸笔进行检查。

不是……您的输入是否包含大括号以外的字符?@Kaidulisam您好,没有,我的输入仅包含大括号。编辑很好,但还不够然而,我们的想法是,我们应该能够复制和粘贴代码,编译代码,并看到您看到的相同内容。获取一个最小的示例本身就是一种基本的调试技术。在关键位置添加日志输出,以找出程序流偏离预期的地方也很有帮助。换句话说,您尝试过什么下一步,如果你的代码中包含关于你不想对给定结构做什么的注释,那么就更容易发现你的代码没有按预期工作的地方。没有注释,我们必须先弄清楚你的代码做了什么,然后弄清楚这样做是否有意义,而不需要任何关于你打算做什么的提示。(给出意外结果的代码证明代码和意图在某些地方有所不同。;-)你似乎已经放弃了对括号和大括号的支持。我认为OP使用了偏执一词[sic]是的,我已经解决了这个问题,并且刚刚从我的存档中粘贴。我想OP将能够通过修改问题的标记来处理另外两个括号:问题是标记C,而不是C++。