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 <cstring>
 4
 5 const int maxn = 100000 + 10;
 6 int last, cur, next[maxn];
 7 char s[maxn];
 8
 9 int main(void)
10 {
11     #ifdef LOCAL
12         freopen("11988in.txt", "r", stdin);
13     #endif
14
15     while(scanf("%s", s + 1) == 1)
16     {
17         int n = strlen(s + 1);
18         last = cur = 0;
19         next[0] = 0;
20
21         for(int i = 1; i <= n; ++i)
22         {
23             char ch = s[i];
24             if(s[i] == ‘[‘)    cur = 0;
25             else if(s[i] == ‘]‘)    cur = last;
26             else
27             {
28                 next[i] = next[cur];
29                 next[cur] = i;
30                 if(cur == last)    last = i;    //更新最后一个字符编号
31                 cur = i;                    //移动光标
32             }
33         }
34
35         for(int i = next[0]; i != 0; i = next[i])
36             printf("%c", s[i]);
37         puts("");
38     }
39
40     return 0;
41 }

代码君

时间: 2024-12-18 17:14:10

UVa 11988 (数组模拟链表) 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

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

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

UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) 题解

刘汝佳的题目,悲剧文本 -_-||| 这里使用vector<string>容器倒置记录数据,然后从后面输出就可以了. 难度就是不知道这样的文档到底哪里是开始输出,故此使用动态管理内存的容器比较好做. 增加了io处理的O(n)算法也没有上榜,郁闷. #include <stdio.h> #include <vector> #include <string> using std::vector; using std::string; const int MAX_

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

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

B - 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 automatically pres

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