c++ - 错误答案,无法找出我的代码有什么问题
问题描述
我已经用基本的字符串方法完成了这个问题,我正在用堆栈尝试这个问题,但即使我的所有测试用例都正常工作,也会得到错误的答案。问题链接: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;
}
}
解决方案
推荐阅读
- sql - 从可以是正数和负数的值创建 %
- macos - 尝试在 MacOS 上安装 OpenEdx Devstack 时出现权限错误
- powershell - 似乎无法将文件中的 base64 字符串用于批量变量
- json - 如何将 JSON 存储在 Drone 中并将其写入文件而不会出现格式错误?
- reactjs - 如何监视 Web 版 FireBase 表中嵌套对象的变化
- java - 对多维数组进行算术运算时,有没有办法格式化数组输出
- python - Pandas - 通过保持优先值,根据系列中的条件从 DataFrame 中删除重复的行
- workday-api - 如何从 WID 获取指向 Workday 中特定候选人的 URL
- javascript - 检查输入数字是否有小数
- powershell - 使用 powershell 直接从 SQL 数据透视表