链表和模拟链表,双向模拟链表
通过链表去存储一串数字
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int n;
scanf("%d", &n);
struct node *head, *p, *q;
head = NULL;
for(int i = 1; i <= n; i++)
{
int a;
scanf("%d", &a);
p = (struct node *)malloc(sizeof(struct node));
p->data = a;
p->next = NULL;
if(head == NULL)
head = p;
else
q->next = p;
q = p;
}
struct node *t;
t = head;
while(t != NULL)
{
printf("%d ", t->data);
t = t->next;
}
return 0;
}
输入
9
2 3 5 8 9 10 18 26 32
输出
2 3 5 8 9 10 18 26 32
通过模拟链表去存储一串数字
#include <bits/stdc++.h>
using namespace std;
int main()
{
int data[101], next[101];
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &data[i]);
int len = n;
for(int i = 1; i <= n; i++)
{
if(i!=n)
next[i] = i+1;
else
next[i] = 0;
}
int t = 1;
while(t != 0)
{
printf("%d ", data[t]);
t = next[t];
}
return 0;
}
输入输出也上述结果是一致的,而且在代码实现上,模拟链表更为简单和方便
有序数组中,插值
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int n;
scanf("%d", &n);
struct node *head, *p, *q;
head = NULL;
for(int i = 1; i <= n; i++)
{
int a;
scanf("%d", &a);
p = (struct node *)malloc(sizeof(struct node));
p->data = a;
p->next = NULL;
if(head == NULL)
head = p;
else
q->next = p;
q = p;
}
int b;
scanf("%d", &b);
struct node *t;
t = head;
while(t != NULL)
{
if(t->next->data > b)
{
p = (struct node *)malloc(sizeof(struct node));
p->data = b;
p->next = t->next;
t->next = p;
break;
}
t = t->next;
}
t = head;
while(t != NULL)
{
printf("%d ", t->data);
t = t->next;
}
return 0;
}
输入
9
2 3 5 8 9 10 18 26 32
11
输出
2 3 5 8 9 10 11 18 26 32
通过模拟链表进行插值
#include <bits/stdc++.h>
using namespace std;
int main()
{
int data[101], next[101];
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &data[i]);
int len = n;
for(int i = 1; i <= n; i++)
{
if(i!=n)
next[i] = i+1;
else
next[i] = 0;
}
len++;
scanf("%d", &data[len]);
int t = 1;
while(t != 0)
{
if(data[next[t]] > data[len])
{
next[len] = next[t];
next[t] = len;
break;
}
t = next[t];
}
t = 1;
while(t != 0)
{
printf("%d ", data[t]);
t = next[t];
}
return 0;
}
使用模拟链表也可以实现双向链表和循环链表
破损键盘
你有一个破损的键盘。键盘上所有的键都可以正常工作,但有时候Home键或者End键会自动按下。你并不知道键盘存在这一问题,而是专心打稿子,甚至连显示器都没打开。当你打开显示器后,展现在你面前的是一段悲剧文本。你的任务是在打开显示器之前计算出这段悲剧文本。
输入包含多组数据。每组数据占一行,包含不超过100000个字母、下划线、字符“【”或者“】”。其中字符“【”表示Home键,“】”表示End键。输入结束标志为文件结束符(EOF)。输入文件不超过5MB。对于每组数据,输出一行,即屏幕上的悲剧文本。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
int last, cur, next[maxn];
char s[maxn];
int main()
{
while(scanf("%s", s+1) == 1)
{
int n = strlen(s+1);
last = cur = 0;
next[0] = 0;
for(int i = 1; i <= n; i++)
{
char ch = s[i];
if(ch == '[') cur = 0;
else if(ch == ']') cur = last;
else
{
next[i] = next[cur];
next[cur] = i;
if(cur == last)last = i;
cur = i;
}
}
for(int i = next[0]; i != 0; i = next[i])
printf("%c", s[i]);
printf("\n");
}
return 0;
}
输入
This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University
输出
BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University
这个题用模拟链表去解题的
移动盒子
你有一行盒子,从左到右依次编号为1, 2, 3,…, n。可以执行以下4种指令:
1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令)。
2 X Y表示把盒子X移动到盒子Y右边(如果X已经在Y的右边则忽略此指令)。
3 X Y表示交换盒子X和Y的位置。
4表示反转整条链。
指令保证合法,即X不等于Y。例如,当n=6时在初始状态下执行114后,盒子序列为2 3 1 4 5 6。接下来执行2 3 5,盒子序列变成2 1 4 5 3 6。再执行3 1 6,得到2 6 4 5 3 1。最终执行4,得到1 3 5 4 6 2。
输入包含不超过10组数据,每组数据第一行为盒子个数n和指令条数m(1≤n,m≤100000)(1≤n,m≤100000)(1≤n,m≤100000),以下m行每行包含一条指令。每组数据输出一行,即所有奇数位置的盒子编号之和。位置从左到右编号为1~n。
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=100000+5;
int n,leftt[maxn],rightt[maxn];//left and right is ambiguous
void link(int L,int R)
{
//对于左边的节点来说,左边的左边本来就已经连接上了,还差右方向的链接没连上
rightt[L]=R;
//同理对于右边的节点,链接左边
leftt[R]=L;
}
int main()
{
int m,kase=0;
while(scanf("%d%d",&n,&m)==2){
for(int i=1;i<=n;i++){
leftt[i]=i-1;
rightt[i]=(i+1)%(n+1);
}
rightt[0]=1;leftt[0]=n;
int op,X,Y,inv=0;
while(m--){
scanf("%d",&op);
if(op==4) inv=!inv;
else{
scanf("%d%d",&X,&Y);
if(op==3&&rightt[Y]==X) swap(X,Y);
//!为了转化为同一种情况来处理,x、y只是一个代号,反正结果变得是值,就是挨着的
if(op!=3&&inv) op=3-op;
//如果操作1,将X移动到Y的左边,如果已经在左边了,则跳过操作
if(op==1&&X==leftt[Y]) continue;
//如果操作2,将X移动在Y的右边,如果已经在右边了,则跳过操作
if(op==2&&X==rightt[Y]) continue;
//记录出X,Y的左边和右边
int LX=leftt[X],RX=rightt[X],LY=leftt[Y],RY=rightt[Y];
//下面分类讨论,不要漏掉
if(op==1){
link(LX,RX);link(LY,X);link(X,Y);
}//X的左右连起来,X和Y左,X和Y
else if(op==2){
link(LX,RX);link(Y,X);link(X,RY);
}//X的左右连起来,Y和X,X和Y右 按照链的顺序来写,可能思路更清晰
else if(op==3){
if(rightt[X]==Y) {link(LX,Y);link(Y,X);link(X,RY);}
else {link(LX,Y);link(Y,RX);link(LY,X);link(X,RY);}
}
}
}
int b=0;
long long ans=0;
for(int i=1;i<=n;i++){
b=rightt[b];//向右推移
if(i%2==1) ans+=b;
}
if(inv&&n%2==0) ans=(long long)n*(n+1)/2-ans;//如果n不是偶数的话,倒一下结果也一样
printf("Case %d: %lld\n",++kase,ans);
}
return 0;
}
输入
6 4
1 1 4
2 3 5
3 1 6
4
输出
Case 1: 12
这题采用双向链表这样的复杂链式结构,使用辅助函数设置链接关系,op=4,操作最为特殊
操作1,2,3如图解所示