UVa11988 Broken Keyboard (a.k.a. Beiju Text) (链表)

链接:http://acm.hust.edu.cn/vjudge/problem/18693分析:链表。用nxt[i]保存s[i]右边字符在s中的下标。增加一个虚拟字符s[0],那么nxt[0]就是显示屏中最左边的字符。cur表示光标位置左边的字符在s数组中的下标,last表示显示屏中最后一个字符在s数组中的下标。其实就是用nxt链表重新组织s数组的输出顺序,输入一个字符,先将新插入字符的nxt[i]指向nxt[cur],再将nxt[cur]指向i,就是在链表元素cur后插入元素i,更新最后一个字符的编号也就是链表中最后一个元素,移动光标位置,移动到新插入元素(插入字符在s中的下标)结点的位置。‘[‘移动到最左边cur=0,‘]‘移动到最后。
 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4
 5 const int maxn = 100000 + 5;
 6
 7 int cur, last, nxt[maxn];
 8 char s[maxn];
 9
10 int main() {
11     while (scanf("%s", s + 1) == 1) {
12         int n = strlen(s + 1);
13         last = cur = 0;
14         nxt[0] = 0;
15         for (int i = 1; i <= n; i++) {
16             if (s[i] == ‘[‘) cur = 0;
17             else if (s[i] == ‘]‘) cur = last;
18             else {
19                 nxt[i] = nxt[cur];
20                 nxt[cur] = i;
21                 if (cur == last) last = i;
22                 cur = i;
23             }
24         }
25         for (int i = nxt[0]; i; i = nxt[i])
26             printf("%c", s[i]);
27         printf("\n");
28     }
29     return 0;
30 }
时间: 2024-10-06 05:40:23

UVa11988 Broken Keyboard (a.k.a. Beiju Text) (链表)的相关文章

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

Uva11988 Broken Keyboard (a.k.a. Beiju Text)(就是先输出括号的字符)

Description Problem 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 automa

UVA11988 Broken Keyboard (a.k.a. Beiju Text)

看到大一练习题,睡前水一水~~~ Problem 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 a

UVA-11988 Broken Keyboard (a.k.a. Beiju Text) (链表 或 递归)

题目大意:将一个字符串改变顺序后输出.遇到“[”就将后面内容拿到最前面输出,遇到“]”就将后面的内容拿到最后面输出. 题目分析:用nxt[i]数组表示i后面的字符的下标,实际上就是以字符i为头建立链表,写法类似链式前向星. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std; char p[100005]

【日常学习】【非指针链表】Uva11988 - Broken Keyboard (a.k.a. Beiju Text)题解

这道题目拖了好几天,因为鄙人有两大天敌--链表和树TUT看了这个题材知道原来链表可以不用指针写,不过原理也是一样的,相当于是用数组模拟了个链表而不实用结构体,结构体里的指针就换成了两个变量cur和last了.这道题目本来测出来非常奇怪和合因为UVA AC HDU TLE SPOJ RE我正在奇怪,才发现同名的不同题目有三道TUT 题目的详解已经写在了注释里,上代码: #include<cstdio> #include<cstring> using namespace std; co

uva - Broken Keyboard (a.k.a. Beiju Text)(链表)

11988 - 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 problemwith the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed(internally).Yo

N - Broken Keyboard (a.k.a. Beiju Text)

N - Broken Keyboard (a.k.a. Beiju Text) Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description 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 som

Broken Keyboard (a.k.a. Beiju Text) UVA, 11988(链表)

Broken Keyboard (a.k.a. Beiju Text) From:UVA, 11988 Time Limit: 1000 MS 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 "hom

11988 - 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