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. 具体可以再看下英文。。。
题目思路:
模拟题思路一般都很简单,经过几次惨痛的教训,我总结到: 比赛的时候遇到模拟题不要着急入手,一定要想清楚解题的方法和数据结构,不然你会发现越做越难,到最后不得不放弃,同时可以考虑模块化,先从简单的开始,比如这道题的字符数量限制,先不做考虑,等写出全部代码,再加上这一条便会很简单(代码风格很差的例外...)。
通常,模拟题不需要考虑时间复杂度和空间复杂度,只要不太过分,可以随便开,不要畏首畏尾,这也是教训。
这道题,因为经常进行删除和插入操作,我采用的是链表结构,这样代码写起来会容易很多。其他的也没什么了,很水的一道题。
#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的更多相关文章
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 B Mission Impossible 6
#1228 : Mission Impossible 6 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You must have seen the very famou ...
- 2015 ACM-ICPC国际大学生程序设计竞赛北京赛区网络赛 1002 Mission Impossible 6
题目链接: #1228 : Mission Impossible 6 解题思路: 认真读题,细心模拟,注意细节,就没有什么咯!写这个题解就是想记录一下rope的用法,以后忘记方便复习. rope(块状 ...
- hdu 3272 Mission Impossible
Mission Impossible Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hihocoder 1228 Mission Impossible 6
题意:一个模拟……大概就是模拟一个编辑文档的过程……具体的还是读题吧…… 解法:刚开场就发现的一个模拟……果断敲起来…… 要注意几点与实际编辑过程不同: 1.当用C操作标记一段字符后,只有D会改变这段 ...
- 2015北京网络赛B题 Mission Impossible 6
借用大牛的一张图片:模拟 #include<cstdio> #include<cmath> #include<cstring> #include<algori ...
- hiho Mission Impossible 6(模拟 未提交验证。。)
题意:模拟文本操作 思路:模拟 #include<iostream> #include<stdio.h> #include<string.h> using name ...
- 华为5G空口新技术(2015年)
2015-03-24 长江后浪推前浪,4G建设方兴未艾,业界关于5G的讨论已如火如荼.对于每一代移动通信,空口技术都相当于王冠上的明珠. 在月初的世界移动通信大会上,华为发布了面向5G的新空口,并展出 ...
- 小型单文件NoSQL数据库SharpFileDB初步实现
小型单文件NoSQL数据库SharpFileDB初步实现 我不是数据库方面的专家,不过还是想做一个小型的数据库,算是一种通过mission impossible进行学习锻炼的方式.我知道这是自不量力, ...
- Unix及类Unix系统文本编辑器的介绍
概述 Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.VIM是纯粹的自由软件. Vim普遍被推崇为类Vi编辑器中最好的一个,事实上真正的劲敌来自Em ...
随机推荐
- 移动端rem flexible方案
一.px 自动转换为rem sublim Text3 下载本项目,比如:git clone https://github.com/flashlizi/cssrem 进入packages目录(在Subl ...
- [docker][win10]安装的坑
右键这个小图标,先signin,注意这里是ID 不是邮箱 image.png 可能starting 时候就报错说 “Containers feature is not enabled” 或者 ...
- 弱省胡策 Magic
弱省胡策 Magic 求\(n\)个点\(n\)的条边的简单联通图的个数. 毒瘤,还要写高精. 我们枚举环的大小\(k\),\(\displaystyle ans=\sum_{k=3}^nC_n^k ...
- 获取列表的索引操作:enumerate
通过循环获取列表的索引操作: 主要使用:enumerate product_list = [['Iphone7',5800], ['Coffee',30], ['疙瘩汤',10], ['Python ...
- Linux shell判断文件和文件夹是否存在(转发)
#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...
- go中rune和byte的用处
参考:https://www.jianshu.com/p/4fbf529926ca rune是用来区分字符值和整数值的 byte 等同于int8,即一个字节长度,常用来处理ascii字符 rune 等 ...
- SQLNET.AUTHENTICATION_SERVICES操作系统认证登录的设定
$ORACLE_HOME/network/admin/sqlnet.ora 如果使用了SQLNET.AUTHENTICATION_SERVICES=(NTS)则说明可以使用OS认证就,只要conn / ...
- 条件随机场Conditional Random Field-CRF入门级理解
条件随机场Conditional Random Field-CRF入门级理解 有向图与无向图模型 CRF模型是一个无向概率图模型,更宽泛地说,它是一个概率图模型.现实世界的一些问题可以用概率图模型 ...
- python 的__init__ 和__new__ 区别
在此介绍一下 __init__ 和 __new__ 先后调用的区别 代码如下: # __init__ 和 __new__的区别 # 通常在编代码时,__init__ 较为常见,但是__new__却 ...
- 【vue】css解决“防抖动”——防止页面加载图片抖动
overflow:hidden;height:0;padding-bottom:*; // 其中*处填 图片的高宽百分比=高/宽*100%