一些简单的字符串操作C++实现(Text Editor)

7-1 Text Editor(25 分)
(2018年山东ACM程序设计大赛热身赛Problem D)

对于第一次做的提交上去之后出现错误;百般不得其解,最后在网上找了各种方法,找了原题的题解,意思大概是模拟计算机里面的复制粘贴剪贴功能,对于复制,或者剪贴的的字符串,进行粘贴之后得到另一个新的字符串,所有每次剪贴板里面的字符串始终会有变化。明白题目意思之后,我们可以尝试着做题了。第一次的代码明显是简单把题目中的字符串进行操作,不符合题意,所以一直会报错
可能是系统本身原因,显示不了这么多英文,但是我把输入输出结果显示出来了,供大家参考。
Text Editor is a basic computer software, which is widely used to write and view text. The most popular text editor is Notepad in the Windows environment, Textedit in the Mac environment, vi,emacs, gedit in the Linux environment, and edit in DOS environment. Almost all of the text editors allow user to change the content of the text. Let us Suppose string S is the text string (its index starts from 0), and string T is the clipboard. The three operations are described as follows:
COPY | r (0 <=1 <=r <|S|): Copy the substring S|S|+1…Sr to the clipboard T replacing the original content of T. For example: Original S = ‘abcde, T= ‘xy’. After operation COPY 02, newS = ‘abcde’, T= ‘abc’.
2: Cut | r (0 <=1 <=r <|S|): Copy the substring to the clipboard T, replacing the original content of T, then delete substring
For example: Original S = ‘abcde’; T = ‘abc’. After operation CUT 1 2, new S= ‘ade’;T= ‘bc’.

Input:
The input contains only one case. The first line contains a string representing the intial S. The string only contains lowercase letters, and the length of S is always less than 200 (both initially and after operations.) The second line contains an integer m, representing the number of operations (1 <=m <=100). The following m lines represents the operations sequence in the form above.

Output:
The output contains m lines, each contains text string S after corresponding operation.
输入样例:

abcde
4
CUT 1 2
PASTE 0
COPY 0 4
PASTE 1
Sample Ouput:

ade
abcde
abcde
ababcdecde

#include<bits/stdc++.h>
using namespace std;

int main(){
    string str1,str2;
    cin>>str1;
    int length=str1.size();
    int a,m,n,q;
    cin>>a;
    for(int i=0;i<a;i++){
        cin>>str2;
        if(str2=="CUT"){
            cin>>m>>n;
            for(int j=0;j<length;j++){
                if(j<m or j>n){
                   cout<<str1[j];
                }
            }
        }
        else if(str2=="PASTE"){
            cin>>q;
            if(q==0){
                cout<<str1<<endl;
            }else{
                for(int j=0;j<=q;j++){
                cout<<str1[j];
            }
            cout<<str1;
            for(int j=q+1;j<length;j++){
                cout<<str1[j];
            }
            }

        }else if(str2=="COPY"){
            cin>>m>>n;
            for(int j=m;j<=n;j++){
                cout<<str1[j];
            }
        }
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
    string str1,str2,arr1,str4,str6;//str1是剪贴板里面得
    int a,m,n,q,length;
    cin>>arr1;//最终进行操作得字符串
    str6=arr1;
    cin>>a;
    for(int i=0;i<a;i++){
        cin>>str2;
        length=arr1.size();
        if(str2=="CUT"){
            cin>>m>>n;
            string().swap(str1);
            string().swap(str4);
            for(int j=0;j<arr1.size();j++){
                if(j<m or j>n){
                   cout<<arr1[j];
                   str4+=arr1[j];
                }else{
                    str1+=arr1[j];
                }
            }
            //cout<<str4<<"123456789"<<endl;
            arr1=str4;
            printf("\n");
        }

        if(str2=="COPY"){
            cin>>m>>n;
            string().swap(str1);
            for(int j=m;j<=n;j++){
                str1+=arr1[j];
            }
            cout<<arr1<<endl;
        }

        if(str2=="PASTE"){
             cin>>q;
             string().swap(str4);

                for(int j=0;j<=q;j++){
                    cout<<arr1[j];
                    str4+=arr1[j];
                }
                cout<<str1;
                str4+=str1;
                for(int j=q+1;j<arr1.size();j++){
                    cout<<arr1[j];
                    str4+=arr1[j];
                }
                    arr1=str4;

            printf("\n");
        }

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_38505045/article/details/80287054