zoukankan      html  css  js  c++  java
  • 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 "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

    Sample Output

    BeijuThis_is_a__text
    Happy_Birthday_to_Tsinghua_University

    题意

    每次把“[”和"]"里面的东西放到最前面

    题解

    正常暴力不用考虑,TLE

    数组操作的就不说了,这是用链表做的

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct node
     4 {
     5     char data;
     6     struct node *next;
     7 };
     8 int main()
     9 {
    10     char s[100005];
    11     while(scanf("%s",s)!=EOF)
    12     {
    13         node *head=(node*)malloc(sizeof(node)),*rear,*p;
    14         head->next=NULL;
    15         p=rear=head;//第一个对头操作
    16         for(int i=0;s[i];i++)
    17         {
    18             char ch=s[i];
    19             if(ch=='[')
    20                 p=head;
    21             else if(ch==']')
    22                 p=rear;
    23             else
    24             {
    25                 node *q=(node*)malloc(sizeof(node));
    26                 q->data=ch;
    27                 q->next=p->next;
    28                 p->next=q;
    29                 if(p==rear)
    30                     rear=q;
    31                 p=q;
    32             }
    33         }
    34         p=head->next;
    35         while(p)
    36         {
    37             printf("%c",p->data);
    38             p=p->next;
    39         }
    40         printf("
    ");
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    NSOperation, NSOperationQueue 原理探析
    BAT面试的准备—iOS篇
    iOS 导航栏
    iOS应用性能调优的25个建议和技巧
    iOS 性能调优
    构造方法
    属性和成员变量
    iOS消息推送机制
    大道至简,回归到梦开始的地方。人生如此,编程亦如此。
    尊重生存在这个世界上的每一个人(转)
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8457720.html
Copyright © 2011-2022 走看看