首页 > 解决方案 > 错误答案,无法找出我的代码有什么问题

问题描述

我已经用基本的字符串方法完成了这个问题,我正在用堆栈尝试这个问题,但即使我的所有测试用例都正常工作,也会得到错误的答案。问题链接:https ://www.codechef.com/problems/COMPILER 我真的很想学习 DSA,请帮帮我,并就我的编码风格提出一些建议(我认为我写代码的方式非常糟糕)。

这是我的代码:

#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
    string str;
    cin>>str;
    class Node{
        public:
         char value;
         int indexvalue;
    };
    std::stack<Node> f;
    
    int start=0;
    int end=0;
    int k=-1;
    for(int i=0;i<str.size();i++){
        char s = str[i];
        
        if(i == 0){
            if(s == '>'){
                break;
            }
        }
     if(s=='<'){
         Node n;
         n.value=s;
         n.indexvalue=i;
         f.push(n);
     }
     else{
         if(!f.empty()){
         Node tempnode=f.top();
         f.pop();
         int tempstart=tempnode.indexvalue;
         int tempend=i;
         int tempk=tempend-tempstart;
         if(tempk>k){
             k=tempk;
              start=tempstart;
             end=tempend;
         }
         }
        
         
     }
     
    }
    cout<<k+1<<endl;
    
 }
}

标签: c++classstack

解决方案


推荐阅读