n次操作,1 x说明x出现1次,2查询当前出现次数最多次数
不知道哪里有问题,程序会死循环。感谢指导。
输入样例
6
1 1
2
1 2
2
1 2
2
输出样例
1
1
2
#include<iostream>
using namespace std;
template <class T>
struct node{//单向链表模板类
T next,val,cnt;
};
struct node<int> a[1001];
int main(){
int n;
cin>>n;
a[0].val=0;a[0].cnt=0;a[0].next=1;
for(int i=1;i<n;i++){//初始化
a[i].val=0;a[i].next=-1;a[i].cnt=0;
}
for(int i=0;i<n;i++){
int x;
cin>>x;
if(x==1){
int y,p=a[0].next;
cin>>y;
while(1){
if(a[p].val==y){
a[p].cnt++;
break;
}
if(a[p].next==-1)break;
p=a[p].next;
}
if(a[p].next==-1&&a[p].val!=y){//第一次添加
a[p].next=y;
a[y].val=y;
a[y].cnt=1;
}
}else if(x==2){
int ans=0,num=0,p=a[0].next;
while(1){
if(a[p].cnt>ans){
ans=a[p].cnt;
num=a[p].val;
}
if(a[p].next==-1)break;
p=a[p].next;
}
cout<<num<<endl;
}
}
return 0;
}