UVA11988-Broken Keyboard(数组模拟链表)

时间:2024-04-21 12:04:04

Problem UVA11988-Broken Keyboard

Accept: 5642  Submit: 34937

Time Limit: 1000 mSec

UVA11988-Broken Keyboard(数组模拟链表) Problem 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 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.

UVA11988-Broken Keyboard(数组模拟链表) 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).

UVA11988-Broken Keyboard(数组模拟链表) Output

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

UVA11988-Broken Keyboard(数组模拟链表) Sample Input

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

Happy_Birthday_to_Tsinghua_University

UVA11988-Broken Keyboard(数组模拟链表) Sample output

BeijuThis_is_a__text

Happy_Birthday_to_Tsinghua_University

题解:第一次做这个题完全是看着紫书的代码敲的,这一次才算是真的自己做出来,其实就是数组模拟链表,但是由于我的代码能力不好的,做起来不顺。

首先是初始化的问题,Next数组全部是0,然后两个指针,一个now,一个tail前者用来表示当前应该在那个字符,后者表示已经处理过的字符串的真正的末尾。

遇到文本字符,就用链表的方式插入到now的后面,然后now到这个新加入的字符,tail更新是看Next[now]是否为0,是0就赋值tail = now.

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; const int maxn = +;
char str[maxn];
int Next[maxn]; int main()
{
//freopen("input.txt","r",stdin);
while(~scanf("%s",str+)){
int len = strlen(str+);
int now = ,tail = ;
memset(Next,,sizeof(Next));
for(int i = ;i <= len;i++){
if(str[i]!='[' && str[i]!=']'){
Next[i] = Next[now];
Next[now] = i;
now = i;
if(Next[now] == ) tail = now;
}
else if(str[i] == '[') now = ;
else{
now = tail;
}
}
int t = Next[];
while(t != ){
printf("%c",str[t]);
t = Next[t];
}
printf("\n");
}
return ;
}