UVA11988 Broken Keyboard 链表

题目描述:

你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下。

给你一段按键的文本,其中‘[‘表示Home键,‘]‘表示End键,输出这段悲剧的文本。

解题思路

用顺序结构储存会超时 所以用模拟链表来储存

cur表示光标的位置 last表示当前最后一个字符的编号 next[i]表示s[i]后面的字符的编号 为了方便起见在数组的最前面虚拟一个s[0]。代码如下

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;
char s[maxn];
int last,cur,next[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;
}
时间: 2024-10-18 00:10:12

UVA11988 Broken Keyboard 链表的相关文章

UVa11988 Broken Keyboard(悲剧文本)

UVa11988 Broken Keyboard(悲剧文本) 题目链接:UVa11988 题目描述: 输入包含多组数据,每组数据占一行,包含不超过100000个字母.下划线.字符"["或者"]".其中字符"["表示Home键,"]"表示End键.输入结束标志为文件结束符(EOF)输入文件不超过5MB,对于每组数据,输出一行,即屏幕上的悲剧文本 样例输入: This_is_a_[Beiju]_text [[]][]Happy_B

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

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

Description Broken KeyboardYou'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 gets automatically pressed (internally). You're not aware of this iss

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) (链表)

链接: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,更新最后一个

【日常学习】【非指针链表】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