bnuoj 29373 Key Logger(模拟双向队列)

http://www.bnuoj.com/bnuoj/problem_show.php?pid=29373

【题意】:模拟光标输入

【题解】:用双向列表模拟实现,这里用其他模拟会超时,注意内存的释放

【code】:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #include <list>
 6 #include <string>
 7 #include <string.h>
 8 
 9 using namespace std;
10 
11 struct Nod
12 {
13     char ch;
14     Nod * second;
15     Nod * first;
16     Nod()
17     {
18         second=NULL;
19         first=NULL;
20         ch=0;
21     }
22 };
23 Nod * head,*tail,*now,*temp;
24 
25 char str[1000010];
26 
27 int main()
28 {
29     int t,cas=1;
30     scanf("%d",&t);
31     while(t--)
32     {
33         int i;
34         scanf("%s",str);
35         int len = strlen(str);
36         head = new Nod;
37         tail = new Nod;
38         head->second = tail;
39         tail->first = head;
40         now = head;
41         int pos = 0;
42         for(i=0;i<len;i++)
43         {
44             char ch = str[i];
45             if(ch=='<')
46             {
47                 if(now!=head)
48                 {
49                     now = now->first;
50                   //  cout<<" sdfsd"<<endl;
51                 }
52             }
53             else if(ch=='>')
54             {
55                 if(now->second!=tail)
56                 {
57                     now = now->second;
58                 }
59             }
60             else if(ch=='-')
61             {
62                 if(now!=head)
63                 {
64                     now->first->second = now->second;
65                     now->second->first = now->first;
66                     temp = now;
67                     free(temp);
68                     now = now->first;
69                 }
70             }
71             else
72             {
73                 temp = new Nod;
74                 temp->ch = ch;
75                 now->second->first = temp;
76                 temp->second =now->second;
77                 now->second = temp;
78                 temp->first = now;
79                 now = now->second;
80             }
81         }
82         now = head->second;
83         printf("Case %d: ",cas++);
84         while(now!=tail)
85         {
86             printf("%c",now->ch);
87             now = now->second;
88             free(now->first);
89         }
90         free(now);
91         putchar(10);
92     }
93     return 0;
94 }

 

 

posted @ 2013-10-04 14:24  crazy_apple  阅读(247)  评论(0编辑  收藏  举报