UVa 11988 数组模拟链表

题目:在一个没有显示器的电脑上输入一个字符串,键盘坏掉了,会随机的出现home,和end按键,

字符串中‘[‘代表home键(句首),‘]‘代表end键(句尾),问最后输出的字符串的格式。

分析:模拟屏幕操作,移动光标,模拟缓冲区输出操作。

说明:数组模拟链表操作,跟随链表操作,形象化模拟。

 1 // UVa11988 Broken Keyboard
 2 // Rujia Liu
 3 #include<cstdio>
 4 #include<cstring>
 5 const int maxn = 100000 + 5;
 6 int last, cur, next[maxn]; // 光标位于cur号字符之后面
 7 char s[maxn];
 8
 9 int main() {
10   while(scanf("%s", s+1) == 1) {
11     int n = strlen(s+1); // 输入保存在s[1], s[2]...中
12     last = cur = 0;
13     next[0] = 0;
14
15     for(int i = 1; i <= n; i++) {
16       char ch = s[i];
17       if(ch == ‘[‘) cur = 0;
18       else if(ch == ‘]‘) cur = last;
19       else {
20         next[i] = next[cur];
21         next[cur] = i;
22         if(cur == last) last = i; // 更新“最后一个字符”编号
23         cur = i; // 移动光标
24       }
25     }
26     for(int i = next[0]; i != 0; i = next[i])
27       printf("%c", s[i]);
28     printf("\n");
29   }
30   return 0;
31 }
时间: 2024-08-12 03:55:23

UVa 11988 数组模拟链表的相关文章

UVa 11988 (数组模拟链表) Broken Keyboard (a.k.a. Beiju Text)

题意: 模拟一个文本编辑器,可以输入字母数字下划线,如果遇到'['则认为是Home键,如果是']'则认作End键. 问最终屏幕上显示的结果是什么字符串. 分析: 如果在数组用大量的移动字符必然很耗时.所以next数组表示显示屏中s[i]右边的字符编号,变量cur模拟光标,即当前光标位于s[cur]的右边. 变量last记录显示屏最后一个字符的下标. 我理解的连接的情况应该是这样子的: 1 //#define LOCAL 2 #include <cstdio> 3 #include <cs

【日常学习】【SPFA负环+数组模拟链表实现】codevs2645 Spore题解

之前刚刚写了一道"香甜的黄油",是USACO的经典题目了.那道题用SPFA怎么找都过不了,看着别人的PAS轻松过各种拙计.黄学长说最佳方案应当是堆优化的dij,我还没有血,等学了那个之后再写黄油题解吧. 题目: 题目描述 Description 在星系1 的某颗美丽的行星之上.某陈将去标号为N 的星系,从星系g1 到达g2,某陈需要花费c1 的代价[主要是燃料,另外还有与沿途Grox 的势力作战的花费],c1 小于0 则是因为 这样的星系旅行,会给某陈带来收益[来源于物流差价,以及一些

UVa12657 - Boxes in a Line(数组模拟链表)

题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y右边(如果X已经在Y的右边则忽略此指令).3 X Y表示交换盒子X和Y的位置.4 表示反转整条链. 盒子个数n和指令条数m(1<=n,m<=100,000) 题解 用数组来模拟链表操作,对于每个节点设置一个前驱和后继. 1操作是把x的前驱节点和x的后继节点连接,y节点的前驱和x节点连接,x节点和y

UVa 11988 Broken Keyboard(链表的应用)

Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automatically pressed (inter

B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automatically pressed (internally). You're not aware of this iss

PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, y

UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】

Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automatically pressed (inter

UVa 11988 Broken Keyboard(数组模拟链表)

题目链接: https://cn.vjudge.net/problem/UVA-11988 1 /* 2 问题 将一段文本经过一定的规则处理后输出,规则就是[表示home键,表示光标跳到行首,]表示end键,表示光标跳到行末 3 4 解题思路 5 可以发现[]是可以嵌套的,采用纯模拟比较复杂,采用类似KMP中的next数组一样,记录一下每个字符的下一个字符位置,最后 6 输出即可. 7 */ 8 #include<iostream> 9 #include<cstdio> 10 #i

josephus Problem 中级(使用数组模拟链表,提升效率)

问题描述: 在<josephus Problem 初级(使用数组)>中,我们提出了一种最简单直接的解决方案. 但是,仔细审视代码之后,发现此种方案的效率并不高,具体体现在,当有人出局时,遍历数组仍需要对其进行判断, 这无疑做了无用功,降低了代码效率,在人数多时尤其明显. 解决方案: 当有人出局时,考虑将当前出局的人的前一个人(未出局)的下一个人置为当前出局的下一个人(未出局).这样,便确保了每次对counter的增加都是有效的,遍历到的人都是还没有出局的.大大提升了程序的效率.这其实运用了链表