UVA - 11988

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 pressed (internally).

You‘re not aware of this issue, since you‘re focusing on the text and did not even turn on the monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor).

In Chinese, we can call it Beiju. Your task is to find the Beiju text.

Input

There are several test cases. Each test case is a single line containing at least one and at most 100,000 letters, underscores and two special characters ‘[‘ and ‘]‘. ‘[‘ means the "Home" key is pressed internally, and ‘]‘ means the "End" key is pressed internally. The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.

Output

For each case, print the Beiju text on the screen.

Sample Input

This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University

Output for the Sample Input

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University


Rujia Liu‘s Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7 int next[100011];
 8 char str[100010];
 9 int main () {
10     // freopen("1.in","r",stdin);
11     while (scanf("%s",str + 1) != EOF) {
12         memset(next,0,sizeof(next));
13         int cur = 0,last = 0;
14         for (int i = 1,len = strlen(str + 1);i <= len;i++) {
15             if (str[i] == ‘[‘) cur = 0;
16                 else if (str[i] == ‘]‘) cur = last;
17                 else {
18                     next[i] = next[cur];
19                     next[cur] = i;
20                     if (cur == last) last = i;
21                     cur = i;
22                 }
23         }
24         for (int i = next[0];i != 0;i = next[i]) {
25             cout << str[i];
26         }
27         cout << endl;
28     }
29 }

时间: 2024-10-03 19:16:02

UVA - 11988的相关文章

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

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

uva 11988 这题可以看出c++中string效率的底下

用c语言实现 #include <iostream> #include <string.h> #include <stdio.h> using namespace std; typedef struct node { char x; struct node *next; }chan; chan *root = new chan; char a[100010]; int main() { while(scanf("%s" , a) != EOF) {

UVA 11988 链表

之前遇到字典树什么的要不就用指针链表,要不直接上list. 数组链表主要思想和指针差不多. 指针是用*next记录下一个的地址然后形成链. 数组本身开辟空间时便是一个地址所以也可以达到这点. 比如 int a[11];     a[0]=1;   a[1]=2;  a[2]=4; a[4]=3; a[a[0]]=a[1]=2这样就达到了将 1 2 4 3 接到一起. 仔细分析一下数组a的下标再不断增大,但是记录的数据却在更新. 所以可以大致写出 int next[11]; int now=0;

UVA 11988 Broken Keyboard (a.k.a. Beiju Text) STL

题目链接: UVA...... 题目描述: 给出一个字符串, 其中遇到"["是光标到最前, 遇到']'时光标又回到最后, 输出最后的文本 解题思路: 用到STL, 设置变量flag维护光标的位置, 当遇到纯字母时, 组合起来当做字符串处理, 在遇到下一个非纯字母时, 根据flag的状态一起插进deque的最前或者最后 代码: #include <iostream> #include <cstdio> #include <string> #includ

UVa 11988 破损的键盘(链表)

原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3139 题意就是输入文本,若是遇到"["光标就移到最前面,遇到"]"光标就移到最后. 在这段代码中,在for循环中如果不用n来代替strlen(s+1),最后就会超时,以后写代码的时候我会注意到这点. 1 #include<iostre

Uva 11988 Broken Keyboard

因为要经常移动这些字符,所以采用的数据结构是链表. next[i]起到的作用大概就是类似链表里的next指针. 0.需要注意的是,判断cur == last ? 如果 是 则 last=i 1.另外一点是,输入的时候把next[0]空出来,起链表里面空白头结点的作用.所以输入的时候有个小改动 从s+1开始填入字符,计算长度的时候也是从s+1开始计算,s代表的是这个数组的首地址. 2.直接往oj上交的时候要注意,next数组名要改一下 2. 1 while(~scanf("%s",s+1

uva 11988 - Broken Keyboard (a.k.a. Beiju Text)

题意:有一个键盘坏了  会在你不知道的情况下按下home或者end  给你这个键盘的实际输入  要求输出显示器上的实际显示 解析:输入最大5MB  直接数组检索肯定会超时,用 数组模拟链表 next数组表示显示屏中s[i]右边的字符编号,变量cur模拟光标,即当前光标位于s[cur]的右边. 变量last记录显示屏最后一个字符的下标. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 const in

UVA 11988 Broken Keyboard (链表)

简单题,题目要求显然是很多次插入,所以是链表. acm里链表出场率并不高,但是实际应用中还是蛮多的.想想一年前这个时候连c语言都不会,真是感慨... 插入两个语句,nxt[i] = nxt[u] 表示 i结点指向u的后继, nxt[u] = i表示把u的后继设成i. 设置一个头结点,指向一个不存在的结点,维护一下最后一个结点tail. #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+5; int nxt[max