zoukankan      html  css  js  c++  java
  • UVA11988-Broken Keyboard(数组模拟链表)

    Problem UVA11988-Broken Keyboard

    Accept: 5642  Submit: 34937

    Time Limit: 1000 mSec

    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.

     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).

     Output

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

     Sample Input

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

    Happy_Birthday_to_Tsinghua_University

    Sample output

    BeijuThis_is_a__text

    Happy_Birthday_to_Tsinghua_University

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

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

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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 using namespace std;
     6 
     7 const int maxn = 100000+100;
     8 char str[maxn];
     9 int Next[maxn];
    10 
    11 int main()
    12 {
    13     //freopen("input.txt","r",stdin);
    14     while(~scanf("%s",str+1)){
    15         int len = strlen(str+1);
    16         int now = 0,tail = 0;
    17         memset(Next,0,sizeof(Next));
    18         for(int i = 1;i <= len;i++){
    19             if(str[i]!='[' && str[i]!=']'){
    20                 Next[i] = Next[now];
    21                 Next[now] = i;
    22                 now = i;
    23                 if(Next[now] == 0) tail = now;
    24             }
    25             else if(str[i] == '[') now = 0;
    26             else{
    27                 now = tail;
    28             }
    29         }
    30         int t = Next[0];
    31         while(t != 0){
    32             printf("%c",str[t]);
    33             t = Next[t];
    34         }
    35         printf("
    ");
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    [Ramda] Getter and Setter in Ramda & lens
    [Angular2 Router] Index router
    [TypeScript] Using Interfaces to Describe Types in TypeScript
    [RxJS] ReplaySubject with buffer
    [Ramda] Declaratively Map Predicates to Object Properties Using Ramda where
    [Ramda] Pluck & Props: Get the prop(s) from object array
    [Ramda] Complement: Logic opposite function
    [TypeScript] Distinguishing between types of Strings in TypeScript
    [NodeJS] Use Now alias for custom sub-domains
    [CSS3] Create a fixed-fluid-fixed layout using CSS calc()
  • 原文地址:https://www.cnblogs.com/npugen/p/9501320.html
Copyright © 2011-2022 走看看