記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
while(cin>>t)
{
priority_queue<int> p;
queue<int> q;
stack<int> s;
bool f=1,f2=1,f3=1;
while(t--)
{
int r,m;
cin>>r>>m;
if(!f&&!f2&&!f3) continue;
if(r==1) q.push(m),s.push(m),p.push(m);
else
{
if(q.front()!=m||q.empty()) f=0;
if(s.top()!=m||s.empty()) f2=0;
if(p.top()!=m||p.empty()) f3=0;
if(f) q.pop();
if(f2) s.pop();
if(f3) p.pop();
}
}
if(f+f2+f3>1)
{
cout<<"not sure\n";
}
else if(f) cout<<"queue\n";
else if(f2) cout<<"stack\n";
else if(f3) cout<<"priority queue\n";
else cout<<"impossible\n";
}
}
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
while(cin>>t)
{
priority_queue p;
queue q;
stack s;
bool f=1,f2=1,f3=1;
while(t--)
{
int r,m;
cin>>r>>m;
if(!f&&!f2&&!f3) continue;
if(r==1) q.push(m),s.push(m),p.push(m);
else
{
if(q.front()!=m||q.empty()) f=0;
if(s.top()!=m||s.empty()) f2=0;
if(p.top()!=m||p.empty()) f3=0;
if(f) q.pop();
if(f2) s.pop();
if(f3) p.pop();
}
}
if(f+f2+f3>1)
{
cout<<"not sure\n";
}
else if(f) cout<<"queue\n";
else if(f2) cout<<"stack\n";
else if(f3) cout<<"priority queue\n";
else cout<<"impossible\n";
}
}
要先判斷empty(),如以下:
if(q.empty()||q.front()!=m) f=0;
if(s.empty()||s.top()!=m) f2=0;
if(p.empty()||p.top()!=m) f3=0;