题目: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. CF917D Stranger Trees

    CF917D Stranger Trees 题目描述 给定一个树,对于每个\(k=0,1\cdots n-1\),问有多少个生成树与给定树有\(k\)条边重合. 矩阵树定理+高斯消元 我们答案为\(f ...

  2. static 关键字和类的加载顺序

      静态变量在类加载时初始化,而非静态变量在创建对象时初始化.static关键字修饰的变量就是静态变量. 子类继承父类,子类在生成对象的时候,先初始化父类的成员变量,接着执行父类的构造器,完成父类的初 ...

  3. [MySQL性能优化系列] 聚合索引

    1. 普通青年的索引使用方式 假设我们有一个用户表 tb_user,内容如下: name age sex jack 22 男 rose 21 女 tom 20 男 ... ... ... 执行SQL语 ...

  4. 解决y7000笔记本ubuntu18.04下 休眠挂起后唤醒花屏

    定位问题,切换到核显后发现一点问题也没有,基本确定是显卡驱动的问题 但是由于配置环境比较复杂,不想重新装N卡驱动,所以另寻方法 sudo gedit /etc/default/grub 修改前 # I ...

  5. 自然语言处理之LDA主题模型

    1.LDA概述 在机器学习领域,LDA是两个常用模型的简称:线性判别分析(Linear Discriminant Analysis)和 隐含狄利克雷分布(Latent Dirichlet Alloca ...

  6. SQL Access Advisor in Oracle Database 10g

    The SQL Access Advisor makes suggestions about indexes and materialized views which might improve sy ...

  7. P1217 [USACO1.5]回文质数 Prime Palindromes(技巧+暴力枚举+线性筛)

    技巧:就是偶数位的回文数字一定不是质数---------证明:奇数位之和sum1==偶数位之和sum2的数字可以被11整除.(11除外,这是一个坑点) 最高位,最低位必须是 1, 3, 7, 9 暴力 ...

  8. day08--文件操作(2)

    一.with open(): 形式:with open('文件路径(文件名)','文件操作方式','字符编码方式')as 文件别名: with open操作可以将文件的内存释放交给with 管理,wi ...

  9. 输出 1-100 内的奇数和偶数,并对其分别求和(while嵌套if-else循环)

    package com.summer.cn; /** * @author Summer * 输出 1-100 内的奇数和偶数,并对其分别求和 * while嵌套if-else循环 */ public ...

  10. Mybatis学习总结(五)——动态sql

    MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...