BUPT复试专题—字符串处理(2016)

题目描述

有以下三种操作。

(1)COPY l r(0<=l<=r<n),n代表s串的长度。这个表示将s串从l到r的序列复制到剪贴板t里面,覆盖t字符串。

例如s为abcde    t为pqr

执行COPY 1 2变为

s为abcde     t为bc

(2)CUT l r(0<=l<=r<n),n代表s串的长度。这个表示将s串从l到r的序列剪切到剪贴板t里面(删除s串中的l到r的序列),覆盖t字符串。

例如s为abcde    t为pqr

执行CUT 1 2变为

s为ade     t为bc

(3)PASTE p(0<=p<n),n代表s串的长度。这个表示将t串插入到s串p位置的后面。t保持不变。

例如s为abcde    t为pqr

执行PASTE 1 变为

s为abpqrcde      t为pqr

输入

输入正整数N,表示N例测试。首先给你s串,再给你一个m,然后给你m个操作。

输出

对每个操作,输出操作后的s串。

样例输入

abcde
5
CUT  1 2
COPY 0 1
PASTE 1
PASTE 1
CUT  1 3

样例输出

ade
ade
adade
adadade
aade

来源

2016机考D题

#include<iostream>
#include<cstring>
#define maxn 1005
using namespace std;
 
char s[maxn];
char t[maxn];
char tmp[maxn];
char op[15];
 
int main()
{
    int m,i;
    int l,r,p;
 
    while(cin>>s)
    {
        cin>>m;
        strcpy(t,"");
        while(m--)
        {
            cin>>op;
            if(strcmp(op,"COPY")==0)     //COPY
            {
                cin>>l>>r;
                for(i=l; i<=r; i++)
                {
                    tmp[i-l]=s[i];
                }
                tmp[r-l+1]='';
                strcpy(t,tmp);
                cout<<s<<endl;
            }
            else if(strcmp(op,"CUT")==0)
            {
                cin>>l>>r;
                for(i=l; i<=r; i++)
                {
                    tmp[i-l]=s[i];
                }
                tmp[r-l+1]='';
                strcpy(t,tmp);
 
                strcpy(tmp,"");
 
                int len1=strlen(s);
                int len2=r-l+1;
                for(i=0; i<l; i++)
                    tmp[i]=s[i];
                for(i=l; i<len1-len2; i++)
                    tmp[i]=s[i+len2];
                tmp[i]='';
                strcpy(s,tmp);
                cout<<s<<endl;
            }
            else
            {
                cin>>p;
                int len1=strlen(s);
                int len2=strlen(t);
                for(i=0; i<=p; i++)
                    tmp[i]=s[i];
                for(i=p+1; i<=p+len2; i++)
                    tmp[i]=t[i-p-1];
                for(i=p+1+len2; i<len1+len2; i++)
                    tmp[i]=s[i-len2];
                tmp[i]='';
                strcpy(s,tmp);
                cout<<s<<endl;
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dzzy/p/8484681.html