zoukankan      html  css  js  c++  java
  • Broken Keyboard(模拟数组或者双重链表的运用)

    这题我是大写的服气,辛辛苦苦搞了个双重链表结果还一直不对,不对就算了,书上源代码打进去还是不对,我能怎么办我也很无奈。不过这题还是让我对双重链表更加了解和运用了!还是可以的!

    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

    附上我的代码加正确答案

      1 #include<iostream>
      2 #include<set>
      3 #include<cstring>
      4 #include<cstdio>
      5 #include<cmath>
      6 #include<algorithm>
      7 using namespace std;
      8 struct date
      9 {
     10     char ch;
     11     date *next;
     12 };
     13 class myqueue
     14 {
     15 public:
     16 
     17     date *top,*tail,*mid;
     18     myqueue()
     19     {
     20         top=NULL;
     21         tail=NULL;
     22     }
     23     void inserts(char x)
     24     {
     25         date *p;
     26         p=new date;
     27         p->ch=x;
     28         p->next=NULL;
     29         if(top==NULL)
     30         {
     31             top=p;
     32             tail=p;
     33         }
     34         else
     35         {
     36             tail->next=p;
     37             tail=p;
     38 
     39 
     40         }
     41     }
     42     void insertm(char x)
     43 {
     44 
     45     date *p;
     46     p=new date;
     47     p->ch=x;
     48     p->next=mid->next;
     49     mid->next=p;
     50     mid=p;
     51 }
     52     void insertq(char x)
     53     {
     54 
     55         date *p;
     56         p=new date;
     57         p->ch=x;
     58         p->next=NULL;
     59         if(top==NULL)
     60         {
     61              top=p;
     62             tail=p;
     63 
     64 
     65         }
     66         else
     67         {
     68             p->next=top;
     69             top=p;
     70 
     71         }
     72         mid=top;
     73    }
     74    void clears()
     75    {
     76        top=tail=NULL;
     77     }
     78     void print()
     79     {
     80         date *p=top;
     81         while(p!=NULL)
     82         {
     83             cout<<p->ch;
     84             p=p->next;
     85 
     86         }
     87 }
     88 
     89 };
     90 int main()
     91 {
     92     char x[100005];
     93     myqueue s;
     94     while(cin>>x)
     95     {
     96         s.clears();
     97         int flag=1;
     98         int len=strlen(x);
     99         for(int i=0;i<len;i++)
    100         {
    101             if(x[i]=='[')
    102                 flag=0;
    103             else if(x[i]==']')
    104                     flag=1;
    105                     else
    106                     {
    107                         if(flag==1)
    108                             s.inserts(x[i]);
    109                         else if(flag==0)
    110                             {
    111                                 s.insertq(x[i]);
    112                                 flag=2;
    113                            }
    114                          else  if(flag==2)
    115                             s.insertm(x[i]);
    116 
    117   }
    118 
    119 }
    120           s.print();
    121           cout<<endl;
    122 
    123  }
    124 
    125     return 0;
    126 
    127 
    128 }
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 using namespace std;
     5 #define N 100005
     6 int main()
     7 {
     8     char s[N];
     9     int last,cur,next[N];
    10     while(scanf("%s",s+1)==1)
    11     {
    12         int n=strlen(s+1);
    13         last=cur=0;
    14         next[0]=0;
    15         for(int i=1;i<=n;i++)
    16         {
    17             char ch=s[i];
    18             if(ch=='[')
    19                 cur=0;
    20             else if(ch==']')
    21                 cur=last;
    22             else
    23             {
    24                 next[i]=next[cur];
    25                 next[cur]=i;
    26                 if(cur==last)
    27                     last=i;
    28                 cur=i;
    29             }
    30         }
    31         for(int i=next[0];i!=0;i=next[i])
    32             printf("%c",s[i]);
    33         printf("
    ");
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    如何设置范围,使透视数据源记录可以自适应地改变
    Atitit..文件上传组件选择and最佳实践的总结(2)----HTTP
    AIDL(1)
    最好的年龄减肥
    2012在数据库技术会议上的讲话PPT打包
    左右 Java 于 finally 深度分析语句块
    R0-R37它是Arm 寄存器,那是,CPU内部。和GPIO注册所有外设。换句话说,要是arm的cpu,它包含了其他芯片公司将有R0-R37,和GPIO寄存器只有一个特定的芯片。
    使用方便 正则表达式grep,sed,awk(一)
    经验36--C#无名(大事,物...)
    IOS 图片压缩
  • 原文地址:https://www.cnblogs.com/blvt/p/7229015.html
Copyright © 2011-2022 走看看