题目: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. 具体可以再看下英文。。。

题目思路:

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

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

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

 #include<stdio.h>
#include<string.h>
#include<stdlib.h> #define abs(x) (x<0?-x:x) typedef struct stri
{
char c;
int length;
struct stri *pos;
bool add_method;
struct stri *next;
struct stri *last;
}Str; bool cop;
Str *l,*r;
int co=;
char st[];
int lst; void completeCopy(Str *str)
{
lst=;
cop=;
if(co==) st[]=;
else if(co>)
{
l=l->next;
while(l!=r)
{
st[lst++]=l->c;
l=l->next;
}
st[lst++]=l->c;
st[lst]=;
}
else
{
r=r->next;
while(r!=l)
{
st[lst++]=r->c;
r=r->next;
}
st[lst++]=r->c;
st[lst]=;
}
} int Maxlen; Str *Init()
{
Str *str;
str=(Str *)malloc(sizeof(Str));
str->c='@';
str->pos=str;
str->add_method=;
str->length=;
str->next=NULL;
str->last=NULL;
return str;
} void Add(Str *str,char c)
{
if(str->add_method==)
{
if(str->length>=Maxlen) return ;
Str *s;
s=(Str *)malloc(sizeof(Str));
s->c=c;
s->last=str->pos;
s->next=str->pos->next;
if(str->pos->next)
str->pos->next->last=s;
str->pos->next=s;
str->pos=s;
str->length++;
}
else
{
if(str->pos->next==NULL)
{
if(str->length>=Maxlen) return ;
Str *s;
s=(Str *)malloc(sizeof(Str));
s->c=c;
s->last=str->pos;
s->next=str->pos->next;
if(str->pos->next)
str->pos->next->last=s;
str->pos->next=s;
str->pos=s;
str->length++;
}
else
{
str->pos->next->c=c;
str->pos=str->pos->next;
}
}
} void Delete(Str *str)
{
if(cop==)
{
cop=;
if(co<)
{
Str *p=r->next;
str->pos->next=l->next;
if(l->next)
l->next->last=str->pos;
Str *tmp=l->next;
while(p!=tmp)
{
Str *q=p;
p=p->next;
free(q);
}
}
else if(co==);
else
{
str->pos=l;
Str *p=l->next;
l->next=r->next;
if(r->next)
r->next->last=l;
Str *tmp=r->next;
while(p!=tmp)
{
Str *q=p;
p=p->next;
free(q);
}
}
str->length-=(co<)?-co:co;
return ;
}
if(str->pos->next==NULL);
else
{
Str *p=str->pos->next;
str->pos->next=str->pos->next->next;
if(str->pos->next)
str->pos->next->last=str->pos;
free(p);
str->length--;
}
} void Belete(Str *str)
{
if(str->pos->c=='@');
else
{
Str *p=str->pos;
str->pos=str->pos->last;
str->pos->next=str->pos->next->next;
if(str->pos->next)
str->pos->next->last=str->pos;
free(p);
str->length--;
}
} void Display(Str *str)
{
Str *p=str->next;
while(p)
{
printf("%c",p->c);
p=p->next;
}
printf("\n");
} void Paste(Str *str)
{
if(str->add_method==)
{
if(str->length+abs(co)>Maxlen) return ;
}
else
{
int lo=;
Str *p=str->pos;
while(p->next)
{
lo++;
p=p->next;
}
if(str->length+abs(co)-lo>Maxlen) return ;
}
for(int i=;st[i];i++)
{
Add(str,st[i]);
}
} void Clear(Str *str)
{
Str *p=str;
while(p)
{
Str *q=p;
p=p->next;
free(q);
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cop=;
scanf("%d",&Maxlen);
char s[];
scanf("%s",s);
Str *str=Init();
for(int i=;s[i];i++)
{
if(s[i]>='a'&&s[i]<='z')
{
if(cop==)
{
completeCopy(str);
}
Add(str,s[i]);
}
else if(s[i]=='R')
{
if(str->pos->next==NULL) co--;
else str->pos=str->pos->next;
if(cop==)
{
r=str->pos;
co++;
}
}
else if(s[i]=='L')
{
if(str->pos->c=='@') co++;
else str->pos=str->pos->last;
if(cop==)
{
r=str->pos;
co--;
}
}
else if(s[i]=='S')
{
str->add_method^=;
if(cop==)
{
completeCopy(str);
}
}
else if(s[i]=='D')
{
Delete(str);
}
else if(s[i]=='B')
{
if(cop==)
{
completeCopy(str);
}
Belete(str);
}
else if(s[i]=='C')
{
cop^=;
if(cop==)
{
l=str->pos;
r=l;
lst=;
st[]=;
co=;
}
else
{
completeCopy(str);
}
}
else if(s[i]=='V')
{
Paste(str);
if(cop==)
{
completeCopy(str);
}
}
}
if(str->next==NULL) printf("NOTHING");
Display(str);
Clear(str);
}
return ;
}

Mission Impossible 6的更多相关文章

  1. ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 B Mission Impossible 6

    #1228 : Mission Impossible 6 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You must have seen the very famou ...

  2. 2015 ACM-ICPC国际大学生程序设计竞赛北京赛区网络赛 1002 Mission Impossible 6

    题目链接: #1228 : Mission Impossible 6 解题思路: 认真读题,细心模拟,注意细节,就没有什么咯!写这个题解就是想记录一下rope的用法,以后忘记方便复习. rope(块状 ...

  3. hdu 3272 Mission Impossible

    Mission Impossible Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. hihocoder 1228 Mission Impossible 6

    题意:一个模拟……大概就是模拟一个编辑文档的过程……具体的还是读题吧…… 解法:刚开场就发现的一个模拟……果断敲起来…… 要注意几点与实际编辑过程不同: 1.当用C操作标记一段字符后,只有D会改变这段 ...

  5. 2015北京网络赛B题 Mission Impossible 6

    借用大牛的一张图片:模拟 #include<cstdio> #include<cmath> #include<cstring> #include<algori ...

  6. hiho Mission Impossible 6(模拟 未提交验证。。)

    题意:模拟文本操作 思路:模拟 #include<iostream> #include<stdio.h> #include<string.h> using name ...

  7. 华为5G空口新技术(2015年)

    2015-03-24 长江后浪推前浪,4G建设方兴未艾,业界关于5G的讨论已如火如荼.对于每一代移动通信,空口技术都相当于王冠上的明珠. 在月初的世界移动通信大会上,华为发布了面向5G的新空口,并展出 ...

  8. 小型单文件NoSQL数据库SharpFileDB初步实现

    小型单文件NoSQL数据库SharpFileDB初步实现 我不是数据库方面的专家,不过还是想做一个小型的数据库,算是一种通过mission impossible进行学习锻炼的方式.我知道这是自不量力, ...

  9. Unix及类Unix系统文本编辑器的介绍

    概述 Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.VIM是纯粹的自由软件. Vim普遍被推崇为类Vi编辑器中最好的一个,事实上真正的劲敌来自Em ...

随机推荐

  1. 从n个数里面选择m个数

    从n个数里面选择m个数 #include<iostream> #include<vector> using namespace std; vector<int> s ...

  2. python学习初始函数

    函数的用途:解决代码的冗余.可读性差.可扩展性差. 函数的一般格式: #函数定义 def mylen(): """计算s1的长度""" s1 ...

  3. Sqlite3并发读写注意事项

    最近项目中涉及到sqlite并发读写的问题,参考一些文档并结合自己的实践,对sqlite3并发问题总结了几点: sqlite3的锁及事务类型 sqlite3总共有三种事务类型:BEGIN [DEFER ...

  4. C和指针 (pointers on C)——第七章:函数(上)

    第七章 函数 这一章对于有一定C的基础的人有一定优秀代码风格的人来说,并非非常虐.关于stdarg宏可能有些陌生.它负责可变參数列表的定义. 总结: 新式风格和旧式风格就不要提了.八百年前的事情. 函 ...

  5. 根据JavaBean创建数据库的操作SQL

    根据JavaBean创建数据库的操作SQL import java.lang.reflect.Field; public class GenerateSQL { public static void ...

  6. springboot RestTemplate请求

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code   1.定义 RestTemplateConfig 配置类 @Configuratio ...

  7. day25 Python __setattr__

    #__getattr__只有在使用点调用属性且属性不存在的时候才会触发 class Foo: x=1 def __init__(self,y): self.y=y def __getattr__(se ...

  8. Oracle查询数据库中所有表的记录数

    1.Oracle查询数据库中所有表的记录数,但是有可能不准建议用第二种方式进行查询 select t.table_name,t.num_rows from user_tables t 2.创建orac ...

  9. linux安装jdk1.8(rpm方式)

    在Oracle官网下载64位的jdk1.8版本 jdk1.8: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloa ...

  10. keepalived+lvs子网掩码造成VIP切换故障 + vrrp_script+track_script

    keepalived+lvs子网掩码造成VIP切换故障 架构:keepalived+lvs ,前端调度器是双主模型 现象:keepalived手动停掉一台,但是虚拟IP不会切换 整体网络是24位 VI ...