题目:
小明打字
小明正使用Microsoft Word打一篇文档,文档只包含a-z的小写字母和空格,在打字过程中可能会一次或多次按下Home键、End键、←方向键、→方向键、Insert键、Backspace键。请编写程序,给定小明在键盘上按键的序列,输出小明屏幕上最终显示的文本。 提示:Home键会将当前光标移至文本开始位置,End键当前光标移至文本尾,←键和→键会使当前光标左移或右移一个位置(如果光标在文档头则无法左移,光标在文档尾则无法右移),Insert键会在插入和替换文本间切换(默认是插入状态),Backspace键会删除当前光标前的一个字符。
输入格式
输入为不超过50000个字符,表示小明的按键序列。包含a-z的小写字母、空格以及字符[、]、{、}、-、=。其中字符“[”表示Home键,“]”表示End键,“{”表示←键,“}”表示→键,“-”表示Insert键,“=”表示Backspace键。
输出格式
输出为在小明屏幕上最终显示的文本。最后一个字母后没有回车或换行。
输入样例1:
jilin[i lofe{{-v-} ] universiti=y
输出样例1:
i love jilin university
输入样例2:
abcd[c-de
输出样例2:
cdecd
输入样例3:
[[]][][]happy=birthday
输出样例3:
happbirthday
输入样例4:
efg[bbb}}=}}}}=[{{{{a
输出样例4:
abbbe
代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct _list{
char ch;
struct _list* next;
struct _list* before;
}mlist;
mlist *head, *rear, *now;
mlist* op(mlist* now, int flag,char c){
if(flag == 0){//add
if(head == rear){
mlist* p = (mlist*)malloc(sizeof(mlist));
p->ch = c;
p->next = NULL;
head->next = p;
p->before = head;
rear = now = p;
return now;
}
else if(rear == now){
mlist* p = (mlist*)malloc(sizeof(mlist));
p->ch = c;
p->next = NULL;
p->before = now;
now->next = p;
rear = now = p;
return now;
}
else{
mlist* p = (mlist*)malloc(sizeof(mlist));
p->ch = c;
p->next = now->next;
p->before = now;
now->next->before = p;
now->next = p;
now = p;
return now;
}
}
else if(flag == 1){//remove
if(head == rear){
mlist* p = (mlist*)malloc(sizeof(mlist));
p->ch = c;
p->next = NULL;
head->next = p;
p->before = head;
rear = now = p;
return now;
}
else if(rear == now){
mlist* p = (mlist*)malloc(sizeof(mlist));
p->ch = c;
p->next = NULL;
p->before = now;
now->next = p;
rear = now = p;
return now;
}
else{
now->next->ch = c;
now = now->next;
return now;
}
}
}
mlist* del(mlist* now){
if(head == rear || head == now){
return now;
}
else if(now == rear){
now->before->next = NULL;
mlist*p = now;
now = rear = now->before;
free(p);
return now;
}
else{
now->next->before = now->before;
now->before->next = now->next;
mlist* p = now;
now = now->before;
free(p);
return now;
}
}
mlist* p_home(mlist* now){
now = head;
return now;
}
mlist* p_end(mlist* now){
now = rear;
return now;
}
mlist* p_left(mlist* now){
if(now->before == NULL){
return now;
}
now = now->before;
return now;
}
mlist* p_right(mlist* now){
if(now->next == NULL){
return now;
}
now = now->next;
return now;
}
int main(){
now = head = rear = NULL;
mlist* p = (mlist*)malloc(sizeof(mlist));
p->next = p->before = NULL;
head = rear = now = p;
int flag = 0;
char c;
while(scanf("%c", &c) == 1 && c != '\n'){
if(c == '['){
now = p_home(now);
}
else if(c == ']'){
now = p_end(now);
}
else if(c == '{'){
now = p_left(now);
}
else if(c == '}'){
now = p_right(now);
}
else if(c == '-'){
flag++;
}
else if(c == '='){
now = del(now);
}
else{
now = op(now, flag%2, c);
}
}
for(mlist* p = head->next; p != NULL; p = p->next){
printf("%c", p->ch);
}
}