hihocoder #1228 : Mission Impossible 6 模拟

本文介绍了一个简单的文本编辑器模拟程序的设计与实现过程。该程序使用C++编写,支持基本的编辑操作如插入、删除、复制及粘贴等,并考虑了光标位置和覆盖模式的影响。通过对每种操作的逐一解析,展示了如何处理这些命令来更新内部数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总结 :1 .题意理解
2. 插入要从后往前枚举更新, 删除要从前往后枚举更新数据结构没学好。。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define maxn 100005
using namespace std;
int flagOve;
int po, copyPoStart, copyPoEnd;
char ans[maxn];
int l, m;
char update[maxn];
char cop[maxn];
int copLen;
void copyEnd()//OK
{
     int L = min(copyPoStart, copyPoEnd), R = max(copyPoStart, copyPoEnd);
     copLen = 0;
     for(int i = L; i < R; i++) cop[copLen++] = ans[i];
     copyPoStart = -1;
}
void L()//OK
{
     if(copyPoEnd > 0) copyPoEnd--;
     if(po > 0) po--;
}
void R()//Ok
{
     if(copyPoEnd < l) copyPoEnd++;
     if(po < l) po++;
}
void S()//OK
{
    copyPoStart = -1;
    flagOve ^= 1;
}
void D()//OK
{
     if(copyPoStart != -1)
     {
        int L = min(copyPoStart, copyPoEnd), R = max(copyPoStart, copyPoEnd);
        for(int i = R, j = L; i < l; i++, j++) ans[j] = ans[i];
        l -= (R - L);
        po = L;
     }
     else{
          for(int i = po; i < l - 1; i++) ans[i] = ans[i + 1];
          if(po != l) l--;
     }
    copyPoStart = -1;
}
void B()//OK
{
    copyPoStart = -1;
    int pre = po - 1;
    if(pre == -1)return ;
    else
    {
        for(int i = pre; i < l - 1; i++) ans[i] = ans[i + 1];
        po--;
        l--;
    }
}
void C() //OK
{
    if(copyPoStart != -1) copyEnd();
    else
    {
       copyPoStart = po;
       copyPoEnd = po;
    }
}
void V()//OK
{
     copyPoStart = -1;
     if(flagOve)
     {
        if(copLen + po <= m)
        {
            for(int i = po; i < copLen + po; i++) ans[i] = cop[i - po];
            l = max(l, copLen + po);
            po += copLen;
        }
     }
     else
     {
        if(copLen + l <= m)
        {
            for(int i = l, j = copLen + l; i >= po; i--, j--) ans[j] = ans[i];
            //for(int i = po, j = copLen + po; i < l; i++, j++) ans[j] = ans[i];
            for(int i = po; i < po + copLen; i++) ans[i] = cop[i - po];
            l += copLen;
            po += copLen;
        }
     }
}
void low(char ss)
{
     copyPoStart = -1;
     if(flagOve) {
          if(po != l){ ans[po] = ss; po++; }
          else if(l != m)
          {
              ans[po] = ss;
              po++;
              l++;
          }
      }
     else
     {
      if(l == m)return;
      for(int i = l; i > po; i--) ans[i] = ans[i - 1];
      ans[po] = ss;
      l++;
      po++;
     }
}
void init()
{
     copyPoStart = -1;
     po = 0;
     l = 0;
     copLen = 0;
     flagOve = 0;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
       scanf("%d %s", &m, update);
       init();
       for(int i = 0; update[i]; i++)
       {
           if(update[i] >= 'a' && update[i] <= 'z') low(update[i]);
           else if(update[i] == 'L') L();
           else if(update[i] == 'R') R();
           else if(update[i] == 'S') S();
           else if(update[i] == 'D') D();
           else if(update[i] == 'B') B();
           else if(update[i] == 'C') C();
           else if(update[i] == 'V') V();
       }
       if(l == 0) printf("NOTHING\n");
       else
       {
         for(int i = 0; i < l; i++)printf("%c", ans[i]);
         printf("\n");
       }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值