1. 程式人生 > >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 (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

題目大意:你在輸入文章的時候,鍵盤上的Home鍵和End鍵出了問題,會不定時的按下。

給你一段按鍵的文字,其中'['表示Home鍵,']'表示End鍵,輸出這段悲劇的文字。

思路:使用連結串列來模擬,遇到Home鍵,就將後邊的文字插入到這段文字的最前邊,遇到

End鍵,就插入到這段文字的最後邊。但是用連結串列會用到指標,過程比較繁瑣。這裡用一個

Next陣列模擬指向,Next[i]表示當前顯示屏中s[i]右邊的字元下標。再用一個cur表示當前

游標的位置,last表示最後一個字元的記錄位置,這樣遇到End鍵,就能直接找到游標下一

個指向的字元位置了。

具體參考:演算法競賽入門經典(第二版)P143~144

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

char s[101000];
int Next[101000];

int main()
{
    int cur,last;//cur為游標位置,last為顯示屏最後一個字元
    while(~scanf("%s",s+1))
    {
        memset(Next,0,sizeof(Next));
        int len = strlen(s+1);
        Next[0] = 0;
        cur = last = 0;
        for(int i = 1; i <= len; i++)
        {
            if(s[i] == '[')
                cur = 0;
            else if(s[i] == ']')
                cur = last;
            else
            {
                //模擬插入連結串列過程
                Next[i] = Next[cur];//第i個字元指向游標位置
                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");
        memset(s,0,sizeof(s));
    }

    return 0;
}


相關推薦

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

UVA11988(破損鍵盤) 使用陣列模擬連結串列插入

  開始刷資料結構相關的題了,在筆試和麵試過程中如果要寫出指標形式的連結串列時間上可能不夠。該題很好地訓練了怎麼使用陣列來模擬連結串列   破損的鍵盤(又名:悲劇文字)(Broken Keyboard(a.k.a. Beiju Text), UVa 1198

UVA11988 Broken Keyboard (a.k.a. Beiju Text):題解

題目連結:https://www.luogu.org/problemnew/show/UVA11988 這題雖說是和連結串列有關的模擬,但其實並不是很需要啊,但蒟蒻用了(說的好聽是練手,說的難聽是太弱),效果海星。 分析: 此題模擬即可,本人估計難度大概提高-,可以直接陣列模

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 ba

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

printf gets cte eof stdio.h contain text 1.0 seve You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The on

B - Broken Keyboard (a.k.a. Beiju Text) 數組模擬鏈表

但是 sin last special 代碼 home all least sample You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only pr

UVA 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 problem with the keyboard is that sometimes the

UVA-11988: Broken Keyboard (a.k.a. Beiju Text)(我的第一個連結串列)

#include<stdio.h> #include<iostream> #include<string> #include<stack> #include<string.h> using namespace st

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

Broken Keyboard(a.k.a.Beiju Text),UVa 11988

#include<iostream> #include<stack> #include<string> using namespace std; int last,cur; char s[100005]; int main(){ i

UVa 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

Broken Keyboard (a.k.a. Beiju Text) (靜態連結串列

【題意】 你有一個破損的鍵盤,鍵盤上所有的鍵都可以正常工作,但有時Home鍵和End鍵會自動按下,你不知道鍵盤存在這樣的問題,當你輸入完成以後螢幕上顯示的是一串混亂的文字,你的任務是要輸出這段混亂的文字是什麼 【輸入格式】 多組輸入,每組輸入一行字串,

連結串列-單向連結串列&&UVa 11988 Broken Keyboard(a.k.a.Beijiu Text)(破損的鍵盤(悲劇文字))的理解與解析

連結串列-單向連結串列&&UVa 11988 Broken Keyboard(a.k.a.Beijiu Text)(破損的鍵盤(悲劇文字)) C++最新的2011標準C++1

UVa11988 Broken Keyboard 損壞的鍵盤list

問題 put this space 損壞 Go include 光標移動 keyboard 題目鏈接:https://vjudge.net/problem/UVA-11988 題目大意: 鍵盤的home鍵和end鍵出現了問題。 在輸入一段文本時,home鍵或end鍵可能

例題4-6 uva12412A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

mes spa new contest ber stat .net sta emp 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 訓練編程的題。 原題中沒有除0的數據,所以別擔心你的代碼是因為除0錯了。 多半跟我一樣。 也是因為沒有+e

紫書——A Typical Homework (a.k.a Shi Xiong Bang Bang Mang) UVA - 12412

題解: 一個大一的C語言大作業。(只是缺少了文件輸入輸出orzz) 主要思想就是一個一個功能實現,從簡單的做起,比如0和4. 但是做了一天都debug找不出哪裡錯了,可以說不熟悉stl導致各種bug。以後做還是用陣列的方法避免隱藏得很厲害的bug。   這個ac不了

UVA - 12412,A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

題巨長,但是不難,一個bug調了好長時間,終於ac了。浮點數加上1e-5;當資料庫空的時候直接輸出0.00(雖然udebug裡是輸出nan);還有選操作4時輸出的單引號應該是英文的,樣例給的是中文的。 傳送門:UVA-12412 AC程式碼:   #include<ios

UVA - 12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)  UVA - 12412  題目傳送門 emmmm,不想表達什麼,udbug上的資料全過,可就是WA。。。。 AC了的程式碼(大佬的程式碼) #incl

UVA11988 Broken Keyboard 連結串列

題目描述: 你在輸入文章的時候,鍵盤上的Home鍵和End鍵出了問題,會不定時的按下。 給你一段按鍵的文字,其中'['表示Home鍵,']'表示End鍵,輸出這段悲劇的文字。 解題思路 用順序結構儲存會超時 所以用模擬連結串列來儲存 cur表示游標的位置 last表示當前

NOI2015模擬8.14A+B

std out 一個 blog sam 數據 -a spl stream Description 對於每個數字x,我們總可以把它表示成一些斐波拉切數字之和,比如8 = 5 + 3, 而22 = 21 + 1,因此我們可以寫成 x = a1 * Fib1 + a2