程序在VS上可以运行,但是ACM在线测试错误提示为“内存申请的太多,超过了限制”,请问各位老师这个怎么优化一下?备注:题目要求为时间限制:3000 ms | 内存限制:65535 KB。
#include <stdlib.h>
#include <string>
#include <stack>
#include <stdio.h>
#include <iostream>
using namespace std;
int main(void)
{
stack<char> S;
int n;
scanf("%D",&n);
string pour;
string *result = new string[n];
bool ifgo = true;
for(int i=0;i<n;i++)
{
cin>>pour;
if(pour[0] == ']' || pour[0]==')')
{
result[i] = "No";
}
else
{
for(int j=0;j<pour.length() && ifgo==true;j++)
{
switch(pour[j])
{
case ']':
if(!S.empty())
{
if(S.top()=='[')
{
S.pop();
}
else
{
result[i] = "No";
ifgo = false;
}
}
break;
case ')':
if(!S.empty())
{
if(S.top()=='(')
{
S.pop();
}
else
{
result[i] = "No";
}
}
break;
case '[':
S.push(pour[j]);
break;
case '(':
S.push(pour[j]);
break;
default:break;
}
}
if(S.empty())
{
result[i] = "Yes";
}
else
{
result[i] = "No";
while(!S.empty())
{
S.pop();
}
}
ifgo = true;
}
}
for(int k=0;k<n;k++)
{
cout<<result[k]<<'\n';
}
delete[] result;
}