我需要您关于此代码的帮助;尽管拥有正确的代码,但我仍然遇到了运行时错误。我知道SIGABRT错误是由于内存过多;你们可以告诉我我的代码出错了吗?如何纠正?运行时错误(SIGABRT)
#include<iostream>
#include<stdlib.h>
#include<string>
int count=0;
int a[1000];
using namespace std;
void check(string *str,int p)
{
int j=0,i,k;
int flag=0;
char *first=new char[p];
char *last=new char[p];
for(int i=0;i<p;i++)
{
first[j]=str[i].front();
last[j]=str[i].back();
j++;
}
for(i=0;i<j;i++)
for(k=0;k<j;k++)
{
if(flag>=2)
{
a[count++]=0;
delete first;
delete last;
return;
}
if(first[i]==last[k])
break;
if(k==j-1)
flag++;
}
if(flag>=2)
{
a[count++]=0;
delete first;
delete last;
return;
}
if(i==j)
a[count++]=1;
delete first;
delete last;
}
int main()
{
int t,p;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>p;
string *str=new string[p];
for(int i=0;i<p;i++)
cin>>str[i];
check(str,p);
delete str;
}
for(int i=0;i<count;i++)
if(a[i]==0)
cout<<"\nThe door cannot be opened.";
else
cout<<"\nOrdering is possible.";
return 0;
}
SIGABRT并不专指“过度使用内存”。你的代码相当复杂,并且完全没有指出你用来重现问题的输入字符串是什么[这可能会影响你的结果是什么] –
代码的解决方案是分离字符串的最后一个字符和第一个字符作为输入。然后取消所有在两个数组中首先和最后一个常见的字符,并且如果没有公共字符,则该标志递增,这将返回incase标志为大于2 – navroze