zoukankan      html  css  js  c++  java
  • Mission Impossible 6

    题目:Mission Impossible 6

    题目链接:http://hihocoder.com/problemset/problem/1228

    题目大意:

       大概就是让我们写一个代码模拟文本编辑器的部分功能,要求实现下面的操作功能:

       1. 小写字母就是输入

       2. L 表示光标左移,R 表示右移,如果超过了当前字符串的范围就忽略

       3. S 表示在插入和覆盖之间的转换,如果在插入状态,就在光标右边插入一个字符,如果是覆盖状态,就覆盖右边的一个字符。

       4. D 表示删除光标右边的一个字符

       5. B 表示删除光标左边的一个字符

       6. C 表示在复制状态和普通状态之间的转换,在进入复制状态的时候,当时光标所在的位置作为位置1,结束复制状态的时候光标所在的位置为位置2,那么这两个坐标之间的字符将被复制,结束复制条件:出现小写字符、出现除了L、R外的大写字符,特别的,如果在复制状态中出现D,那么将删除选中区域的所有字符,这并没有完成复制(以前的复制在转换为复制状态的时候就清空了)。结束复制的字符仍然发挥他本身的作用。

       7. V 表示粘贴,如果粘贴后的结果超过字符数目限制,则操作无效,这一条同样适用于上面任何一种操作。

       8. 具体可以再看下英文。。。

    题目思路:

       模拟题思路一般都很简单,经过几次惨痛的教训,我总结到: 比赛的时候遇到模拟题不要着急入手,一定要想清楚解题的方法和数据结构,不然你会发现越做越难,到最后不得不放弃,同时可以考虑模块化,先从简单的开始,比如这道题的字符数量限制,先不做考虑,等写出全部代码,再加上这一条便会很简单(代码风格很差的例外...)。

       通常,模拟题不需要考虑时间复杂度和空间复杂度,只要不太过分,可以随便开,不要畏首畏尾,这也是教训。

       这道题,因为经常进行删除和插入操作,我采用的是链表结构,这样代码写起来会容易很多。其他的也没什么了,很水的一道题。

      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<stdlib.h>
      4 
      5 #define abs(x) (x<0?-x:x)
      6 
      7 typedef struct stri
      8 {
      9   char c;
     10   int length;
     11   struct stri *pos;
     12   bool add_method;
     13   struct stri *next;
     14   struct stri *last;
     15 }Str;
     16 
     17 bool cop;
     18 Str *l,*r;
     19 int co=0;
     20 char st[10010];
     21 int lst;
     22 
     23 void completeCopy(Str *str)
     24 {
     25   lst=0;
     26   cop=0;
     27   if(co==0) st[0]=0;
     28   else if(co>0)
     29   {
     30     l=l->next;
     31     while(l!=r)
     32     {
     33       st[lst++]=l->c;
     34       l=l->next;
     35     }
     36     st[lst++]=l->c;
     37     st[lst]=0;
     38   }
     39   else
     40   {
     41     r=r->next;
     42     while(r!=l)
     43     {
     44       st[lst++]=r->c;
     45       r=r->next;
     46     }
     47     st[lst++]=r->c;
     48     st[lst]=0;
     49   }
     50 }
     51 
     52 int Maxlen;
     53 
     54 Str *Init()
     55 {
     56   Str *str;
     57   str=(Str *)malloc(sizeof(Str));
     58   str->c='@';
     59   str->pos=str;
     60   str->add_method=0;
     61   str->length=0;
     62   str->next=NULL;
     63   str->last=NULL;
     64   return str;
     65 }
     66 
     67 void Add(Str *str,char c)
     68 {
     69   if(str->add_method==0)
     70   {
     71     if(str->length>=Maxlen) return ;
     72     Str *s;
     73     s=(Str *)malloc(sizeof(Str));
     74     s->c=c;
     75     s->last=str->pos;
     76     s->next=str->pos->next;
     77     if(str->pos->next)
     78       str->pos->next->last=s;
     79     str->pos->next=s;
     80     str->pos=s;
     81     str->length++;
     82   }
     83   else
     84   {
     85     if(str->pos->next==NULL)
     86     {
     87       if(str->length>=Maxlen) return ;
     88       Str *s;
     89       s=(Str *)malloc(sizeof(Str));
     90       s->c=c;
     91       s->last=str->pos;
     92       s->next=str->pos->next;
     93       if(str->pos->next)
     94         str->pos->next->last=s;
     95       str->pos->next=s;
     96       str->pos=s;
     97       str->length++;
     98     }
     99     else
    100     {
    101       str->pos->next->c=c;
    102       str->pos=str->pos->next;
    103     }
    104   }
    105 }
    106 
    107 void Delete(Str *str)
    108 {
    109   if(cop==1)
    110   {
    111     cop=0;
    112     if(co<0)
    113     {
    114       Str *p=r->next;
    115       str->pos->next=l->next;
    116       if(l->next)
    117         l->next->last=str->pos;
    118       Str *tmp=l->next;
    119       while(p!=tmp)
    120       {
    121         Str *q=p;
    122         p=p->next;
    123         free(q);
    124       }
    125     }
    126     else if(co==0);
    127     else
    128     {
    129       str->pos=l;
    130       Str *p=l->next;
    131       l->next=r->next;
    132       if(r->next)
    133         r->next->last=l;
    134       Str *tmp=r->next;
    135       while(p!=tmp)
    136       {
    137         Str *q=p;
    138         p=p->next;
    139         free(q);
    140       }
    141     }
    142     str->length-=(co<0)?-co:co;
    143     return ;
    144   }
    145   if(str->pos->next==NULL);
    146   else
    147   {
    148     Str *p=str->pos->next;
    149     str->pos->next=str->pos->next->next;
    150     if(str->pos->next)
    151       str->pos->next->last=str->pos;
    152     free(p);
    153     str->length--;
    154   }
    155 }
    156 
    157 void Belete(Str *str)
    158 {
    159   if(str->pos->c=='@');
    160   else
    161   {
    162     Str *p=str->pos;
    163     str->pos=str->pos->last;
    164     str->pos->next=str->pos->next->next;
    165     if(str->pos->next)
    166       str->pos->next->last=str->pos;
    167     free(p);
    168     str->length--;
    169   }
    170 }
    171 
    172 void Display(Str *str)
    173 {
    174   Str *p=str->next;
    175   while(p)
    176   {
    177     printf("%c",p->c);
    178     p=p->next;
    179   }
    180   printf("
    ");
    181 }
    182 
    183 void Paste(Str *str)
    184 {
    185   if(str->add_method==0)
    186   {
    187     if(str->length+abs(co)>Maxlen) return ;
    188   }
    189   else
    190   {
    191     int lo=0;
    192     Str *p=str->pos;
    193     while(p->next)
    194     {
    195       lo++;
    196       p=p->next;
    197     }
    198     if(str->length+abs(co)-lo>Maxlen) return ;
    199   }
    200   for(int i=0;st[i];i++)
    201   {
    202     Add(str,st[i]);
    203   }
    204 }
    205 
    206 void Clear(Str *str)
    207 {
    208   Str *p=str;
    209   while(p)
    210   {
    211     Str *q=p;
    212     p=p->next;
    213     free(q);
    214   }
    215 }
    216 
    217 int main()
    218 {
    219   int t;
    220   scanf("%d",&t);
    221   while(t--)
    222   {
    223     cop=0;
    224     scanf("%d",&Maxlen);
    225     char s[10000];
    226     scanf("%s",s);
    227     Str *str=Init();
    228     for(int i=0;s[i];i++)
    229     {
    230       if(s[i]>='a'&&s[i]<='z')
    231       {
    232         if(cop==1)
    233         {
    234           completeCopy(str);
    235         }
    236         Add(str,s[i]);
    237       }
    238       else if(s[i]=='R')
    239       {
    240         if(str->pos->next==NULL) co--;
    241         else str->pos=str->pos->next;
    242         if(cop==1)
    243         {
    244           r=str->pos;
    245           co++;
    246         }
    247       }
    248       else if(s[i]=='L')
    249       {
    250         if(str->pos->c=='@') co++;
    251         else str->pos=str->pos->last;
    252         if(cop==1)
    253         {
    254           r=str->pos;
    255           co--;
    256         }
    257       }
    258       else if(s[i]=='S')
    259       {
    260         str->add_method^=1;
    261         if(cop==1)
    262         {
    263           completeCopy(str);
    264         }
    265       }
    266       else if(s[i]=='D')
    267       {
    268         Delete(str);
    269       }
    270       else if(s[i]=='B')
    271       {
    272         if(cop==1)
    273         {
    274           completeCopy(str);
    275         }
    276         Belete(str);
    277       }
    278       else if(s[i]=='C')
    279       {
    280         cop^=1;
    281         if(cop==1)
    282         {
    283           l=str->pos;
    284           r=l;
    285           lst=0;
    286           st[0]=0;
    287           co=0;
    288         }
    289         else
    290         {
    291           completeCopy(str);
    292         }
    293       }
    294       else if(s[i]=='V')
    295       {
    296         Paste(str);
    297         if(cop==1)
    298         {
    299           completeCopy(str);
    300         }
    301       }
    302     }
    303     if(str->next==NULL) printf("NOTHING");
    304     Display(str);
    305     Clear(str);
    306   }
    307   return 0;
    308 }
  • 相关阅读:
    pat甲级 1155 Heap Paths (30 分)
    pat甲级 1152 Google Recruitment (20 分)
    蓝桥杯 基础练习 特殊回文数
    蓝桥杯 基础练习 十进制转十六进制
    蓝桥杯 基础练习 十六进制转十进制
    蓝桥杯 基础练习 十六进制转八进制
    51nod 1347 旋转字符串
    蓝桥杯 入门训练 圆的面积
    蓝桥杯 入门训练 Fibonacci数列
    链表相关
  • 原文地址:https://www.cnblogs.com/hchlqlz-oj-mrj/p/5071668.html
Copyright © 2011-2022 走看看