提交的时候出segment fault ,我看了也不是数组开小的问题,
#include <bits/stdc++.h>
#include
#include
#include
typedef long long ll;
using namespace std;
const int N=1e6+10;
int check[N];//检查相同元素的个数
queue a;
int main()
{
int p;
cin>>p;
int mx=0;
int k;
int i;
int l;
int temp=0;
for(i=0; i<p; i++)
{
cin>>k;
if(check[k]==0)
{
a.push(k);
check[k]++;
l=a.size();
mx=max(l,mx);
}
else if(check[k]!=0)
{
temp=0;
while(1)
{
if(a.front()==k)
{
a.pop();
a.push(k);
check[k]=1;
temp=1;
l=a.size();
mx=max(l,mx);
break;
}else
{
a.pop();
}
if(temp==1) break;
}
}
}
cout<<mx<<endl;
return 0;
}
typedef long long ll;
using namespace std;
const int N = 1e6 + 10;
int check[N];//检查相同元素的个数
queue<int> a;//stl中是必须加类型的,因为std::queue是模板类
int main()
{
int p;
cin >> p;
int mx = 0;
int k;
int i;
int l;
int temp = 0;
for (i = 0; i < p; i++)
{
cin >> k;
if (check[k] == 0)
{
a.push(k);
check[k]++;
l = a.size();
mx = max(l, mx);
}
else if (check[k] != 0)
{
temp = 0;
while (a.empty() == false)//修改了这个位置,a是空的时候,front是无效的
{
if (a.front() == k)
{
a.pop();
a.push(k);
check[k] = 1;
temp = 1;
l = a.size();
mx = max(l, mx);
break;
}
else
{
a.pop();
}
if (temp == 1) break;
}
}
}
cout << mx << endl;
return 0;
}
queue 你用的是stl的吗,怎么没有带类型,这个代码能编译过?
#include <bits/stdc++.h>
#include <queue>
#include <vector>
typedef long long ll;
using namespace std;
const int N = 1e6 + 10;
int check[N]; //检查相同元素的个数
queue<int> a;
int main()
{
int p;
cin >> p;
int mx = 0;
int k;
int i;
int l;
int temp = 0;
for (i = 0; i < p; i++)
{
cin >> k;
if (check[k] == 0)
{
a.push(k);
check[k]++;
l = a.size();
mx = max(l, mx);
}
else if (check[k] != 0)
{
temp = 0;
while (1)
{
if (a.front() == k)
{
a.pop();
a.push(k);
check[k] = 1;
temp = 1;
l = a.size();
mx = max(l, mx);
break;
}
else
{
a.pop();
}
if (temp == 1)
break;
}
}
}
cout << mx << endl;
return 0;
}