7-1 Text Editor(25 分)题解 谁知道对不对?

题目很简单,剪切复制粘贴。

上题目:

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:

  1. 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'.

3.PASTE p (0 <p <|S|): Paste the content of T after Sp. The content of T does not change. For example: Original S= 'abcde' ,T= 'xy'. After operation PASTE 1, newS= 'abxycde',T= 'xy'. Note, |S| represents the length of string S.

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

abcdecde


我们看题目之后就可以分析了。竟然是剪切复制粘贴 我们不妨解析一下 这三个功能。

第一个: CUT : CUT是一个什么功能呢? 假如 CUT 1 3 那就是说明了 先复制 1~3的字符 然后删除 1~3的字符。

第二个: PASTE : 粘贴 在 所指向位置之后。

第三个: COPY : 复制 [n~m] 的字符。


怎么说呢?可能是掉了大坑 用了find() /笑哭  被学长嘲笑了一顿。后来仓促的改了一下。代码质量不高凑合着看吧。

#include<bits/stdc++.h>
using namespace std;
string s1, temp;
int inp;
void copys() {
int qd, zd;
cin >> qd >> zd;
zd++;
temp = s1.substr(qd, abs(zd - qd));
}


void paste() {
int weizhi;
cin >> weizhi;
s1.insert(weizhi + 1, temp);
}


void CUT() { // 知道为什么不能直接用 find 了吗??/笑哭  反例 abcab  CUT 3 4 结果一find 后 找到的是 1 2 就删错了
int pd, zd;
cin >> pd >> zd;
string st = s1;
char* p = (char *)s1.data();
char* s = new char[s1.size()];
for (int i = pd; i <= zd; i++)
p[i] = '-';
for (int i = 0; *p != 0; p++)
if (*p != '-') s[i++] = *p, s[i] = '\0';
temp = st.substr(pd, abs(zd - pd) + 1);
s1 = s;
}




void solve() {


cin >> s1;
for (cin >> inp; inp--;) {
string pd;
cin >> pd;


if (pd == "COPY") {
copys();
}
else if (pd == "PASTE") {
paste();
}
else if (pd == "CUT") {
CUT();
}
cout << s1 << endl;
}
}


int main() {


solve();
//system("pause");
return 0;
}

猜你喜欢

转载自blog.csdn.net/oshuaifeng/article/details/80293814