• hihocoder 1228 Mission Impossible 6


    题意:一个模拟……大概就是模拟一个编辑文档的过程……具体的还是读题吧……

    解法:刚开场就发现的一个模拟……果断敲起来……

    要注意几点与实际编辑过程不同:

    1.当用C操作标记一段字符后,只有D会改变这段字符,其他操作例如V或输入字符都不会改变这段字符,只会取消标记状态,在当前光标位置进行操作。

    2.在替换模式下,V操作会进行替换粘贴,与传统编辑模式不同。

    3.如果输入操作或者V操作进行后就会超过文档长度,那么就不进行操作。

    大概就这些吧……模拟的过程和按题里说的写就没问题……C的处理麻烦一些,但是题目已经给出了解决方案……

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    using namespace std;
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            int m;//文本最大长度
            scanf("%d", &m);
            string s;//输入序列
            cin >> s;
            string txt;//文本
            int len = s.size();
            int istatus = 0, cstatus = 0;//输入状态:0插入,1替换;复制状态:0nothing,1start
            int now = 0, cp;//now当前光标位置,cp是copy position 1
            string cb;//剪贴板
            for(int i = 0; i < len; i++)
            {
                if(s[i] >= 'a' && s[i] <= 'z')
                {
                    if(cstatus) cstatus = 0;
                    if(istatus == 0 && txt.size() < m)
                        txt.insert(txt.begin() + now++, s[i]);
                    else if(istatus == 1)
                    {
                        if(now < txt.size()) txt[now++] = s[i];
                        else if(txt.size() < m) txt.insert(txt.begin() + now++, s[i]);
    
                    }
                }
                if(s[i] == 'L')
                {
                    if(now) now--;
                }
                if(s[i] == 'R')
                {
                    if(now != txt.size()) now++;
                }
                if(s[i] == 'S')
                {
                    if(cstatus) cstatus = 0;
                    istatus = !istatus;
                }
                if(s[i] == 'D')
                {
                    if(cstatus)
                    {
                        int minp = min(now, cp), maxp = max(now, cp);
                        cstatus = 0;
                        txt.erase(txt.begin() + minp, txt.begin() + maxp);
                        now = minp;
                    }
                    else
                    {
                        if(txt.size())
                        {
                            if(now != txt.size())
                            {
                                txt.erase(txt.begin() + now);
                            }
                        }
                    }
                }
                if(s[i] == 'B')
                {
                    if(cstatus) cstatus = 0;
                    if(now)
                    {
                        txt.erase(txt.begin() + --now);
                    }
                }
                if(s[i] == 'C')
                {
                    if(cstatus)
                    {
                        int minp = min(now, cp), maxp = max(now, cp);
                        cstatus = 0;
                        cb.clear();
                        cb.insert(cb.begin(), txt.begin() + minp, txt.begin() + maxp);
                    }
                    else
                    {
                        cstatus = 1;
                        cp = now;
                    }
                }
                if(s[i] == 'V')
                {
                    if(cstatus) cstatus = 0;
                    if(istatus == 0)
                    {
                        if(txt.size() + cb.size() <= m)
                        {
                            txt.insert(now, cb);
                            now += cb.size();
                        }
                    }
                    else
                    {
                        if(m - now >= cb.size())
                        {
                            txt.erase(txt.begin() + now, txt.begin() + min(now + cb.size(), txt.size()));
                            txt.insert(txt.begin() + now, cb.begin(), cb.end());
                            now += cb.size();
                        }
                    }
                }
            }
            if(txt.size())
                cout << txt << endl;
            else
                puts("NOTHING");
        }
        return 0;
    }
    

      

  • 相关阅读:
    软件工程课程作业(四)--返回一个整数数组中最大子数组的和
    构建之法阅读笔记02
    第三周学习进度
    Delphi 之 编辑框控件(TEdit)
    Delphi 之 标签组件(TLabel组件)
    Delphi 之 第九课 Windows编程
    Delphi 之 第八课 动态数组
    VB API 之 第六课 字体应用三
    VB API 之 第五课 字体之其他函数介绍
    Delphi 之 第七课 字符串操作
  • 原文地址:https://www.cnblogs.com/Apro/p/4826352.html
Copyright © 2020-2023  润新知