C++ 代码
1.数组模拟链表,速度很快
2.用scanf printf不用cin cout 会很快(59ms)
3.注意scanf循环读取单个字符:scanf(" %c", &op)
# include <iostream>
# include <stdio.h>
using namespace std;
const int N = 100010;
int e[N], ne[N], head, idx;
void init()
{
head = -1, idx = 0;
}
void add_to_head(int x)
{
e[idx] = x, ne[idx] = head, head = idx ++;
}
void add(int k, int x)
{
e[idx] = x, ne[idx] = ne[k], ne[k] = idx ++;
}
void remove(int k)
{
ne[k] = ne[ne[k]];
}
int main()
{
int m;
scanf("%d", &m);
init();
while(m --)
{
char op;
int x, k;
scanf(" %c", &op); // 注意这里" %c"
//cin >> op;
if(op == 'H')
{
scanf("%d", &x);
add_to_head(x);
}
else if(op == 'I')
{
scanf("%d%d", &k,&x);
add(k - 1, x);
}
else if(op == 'D')
{
scanf("%d", &k);
if(!k) head = ne[head];
remove(k - 1);
}
}
for (int i = head; i != -1; i = ne[i]) printf("%d ", e[i]);
return 0;
}