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 }
  • 相关阅读:
    TortoiseGit日常使用指南
    Ajax在MVC中的应用
    STL源码学习内存管理
    功能最强大的.Net代码生成器——EasyCode
    TortoiseGit使用入门
    负载均衡时数据包流程详解
    C++异步编程 for VS2011
    (译)一个通用快速的反射方法调用
    移位运算
    使用Autofac在ASP.NET Web API上实现依赖注入
  • 原文地址:https://www.cnblogs.com/blvt/p/7229015.html
Copyright © 2011-2022 走看看