11204: 【原1204】edit
题目
题目描述
author: DS TA 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1204
Description
题目具体描述如下。
Input Format
输入文件一开始包含若干行,用于表示文本内容。最后一行以单独的“**”结束。
接下来包括若干条子命令, 如下:
list:list n1 n2,仅此格式合法,一个合法的list指令表示输出n1到n2这n2-n1+1行。
ins:ins i j str,仅此格式合法,表示从i行第j个位置起插入str这个字符串,具体可以看样例,每次加入字符串长度小于100,注意可以加在行末,此时j为行的长度加1。
del:del i j num,仅此格式合法,表示从i行第j个位置起删除num个字符,数据保证不会删除整行。
quit:quit,退出程序,并且将所有修改后的字符串输出。
每行包含一条子命令。
Output Format
对于每一条子命令,若其不合法,则输入"Error!"并换行(不含"")。
对于一个合法的list指令,输出n2-n1+1行字符串。
对于quit指令,输出全部修改后的字符串。
Sample Input
Hello everyone.
I hope you can have a good time during our Data Structure Class.
Enjoy typing .
******
list 1 1
ins 1 154 s 解释:it's too large for the 1st row.(这不是输入文件的内容!)
ins 4 1 s 解释:it's too large for the line.(这不是输入文件的内容!)
ins 2 73 9 解释:also it's invalid.(这不是输入文件的内容!)
del -1 1 2
ins 3 14 by yourself
list 2 3
quit
Sample Output
Hello everyone.
Error!
Error!
Error!
Error!
I hope you can have a good time during our Data Structure Class.
Enjoy typing by yourself.
Hello everyone.
I hope you can have a good time during our Data Structure Class.
Enjoy typing by yourself.
Limits
1.quit保证在且只在最后一行出现。
2.输入格式保证合法(注意,内容可能不合法)。
3.对于一条不合法的子命令,不进行任何文件编辑操作。
4.保证文本不超过100行,每行内容始终不超过2000个字符。
5.保证对于每次del操作,不会删除整行字符。
6.对于ins的列号i,表示在第i个字符之前插入数值。
7.对于del的列号i,表示从第i个字符开始删除。
*8.不合法内容包括:
1)行号or列号的索引超出范围。
2) 删除字符超过该行剩余字符。
9.约定各操作之间(比如ins int int string)用一个空格隔开 数据保证没有多于空格。
10.推荐将list,ins,del操作分别用一个过程来处理(将代码分作一个个过程可以极大提高逻辑性和可读性,请各位务必养成此习惯)。
ligongzzz's solution
#include "iostream"
#include "cstring"
using namespace std;
int main() {
//所有文件内容
char ch[200][3000] = { 0 };
int row = 0;
//输入
while (true) {
char temp[3000];
cin.getline(temp, 3000);
if (strcmp(temp, "******") == 0)
break;
row++;
strcpy(ch[row], temp);
}
//操作
while (true) {
char op[100];
cin >> op;
if (strcmp(op, "quit") == 0) {
cout << ch[1];
for (int i = 2; i <= row; i++) cout << endl << ch[i];
break;
}
if (strcmp(op, "list") == 0) {
int n1, n2;
cin >> n1 >> n2;
//是否合法
if (n1 > n2 || n1 <= 0 || n2 > row) {
cout << "Error!"<<endl;
continue;
}
for (int i = n1; i <= n2; i++)
cout << ch[i] << endl;
}
if (strcmp(op, "ins") == 0) {
int n1, n2;
char str[3000];
cin >> n1 >> n2;
cin.get();
cin.getline(str, 3000);
if (n1 <= 0 || n1 > row || n2 > strlen(ch[n1])+1) {
cout << "Error!" << endl;
continue;
}
strcat(str, ch[n1] + n2-1);
strcpy(ch[n1] + n2-1, str);
}
if (strcmp(op, "del") == 0) {
int n1, n2, num;
cin >> n1 >> n2 >> num;
if (n1 <= 0 || n1 > row || n2 > strlen(ch[n1]) + 1 || num <0 || num + n2 - 1 > strlen(ch[n1])) {
cout << "Error!" << endl;
continue;
}
strcpy(ch[n1] + n2 - 1, ch[n1] + num + n2 - 1);
}
}
return 0;
}
Neight99's solution
#include <cstring>
#include <iostream>
using namespace std;
template <class elemType>
class list {
public:
virtual void clear() = 0;
virtual int length() const = 0;
virtual void insert(int i, const elemType &x) = 0;
virtual void remove(int i) = 0;
virtual elemType visit(int i) const = 0;
virtual ~list(){};
};
template <class elemType>
class sLinkList : public list<elemType> {
private:
struct Node {
elemType data;
Node *next;
Node(const elemType &x, Node *n = NULL) : data(x), next(n) {}
Node() : next(NULL) {}
~Node() {}
};
Node *head;
int Length;
Node *move(int i) const;
public:
sLinkList();
~sLinkList() {
clear();
delete head;
}
void push(const elemType &x) { insert(Length, x); }
void clear();
int length() const { return Length; }
void insert(int i, const elemType &x);
void remove(int i);
elemType visit(int i) const;
void erase(int i);
void erase(const elemType &x, const elemType &y);
void traverse() const {
Node *cur = head->next;
while (cur) {
cout << cur->data;
cur = cur->next;
}
}
};
template <class elemType>
void sLinkList<elemType>::erase(const elemType &x, const elemType &y) {
Node *p = head->next, *q = head;
while (p) {
if (p->data > x && p->data < y) {
q->next = p->next;
delete p;
p = q->next;
} else {
p = p->next;
q = q->next;
}
}
}
template <class elemType>
typename sLinkList<elemType>::Node *sLinkList<elemType>::move(int i) const {
Node *p = head;
while (i-- >= 0) {
p = p->next;
}
return p;
}
template <class elemType>
sLinkList<elemType>::sLinkList() {
head = new Node;
Length = 0;
}
template <class elemType>
void sLinkList<elemType>::clear() {
Node *p = head->next, *q;
head->next = NULL;
while (p != NULL) {
q = p->next;
delete p;
p = q;
}
Length = 0;
}
template <class elemType>
void sLinkList<elemType>::insert(int i, const elemType &x) {
Node *pos;
pos = move(i - 1);
pos->next = new Node(x, pos->next);
++Length;
}
template <class elemType>
void sLinkList<elemType>::remove(int i) {
Node *pos, *delp;
pos = move(i - 1);
delp = pos->next;
pos->next = delp->next;
pos->next = delp->next;
delete delp;
--Length;
}
template <class elemType>
elemType sLinkList<elemType>::visit(int i) const {
return move(i)->data;
}
template <class elemType>
void sLinkList<elemType>::erase(int i) {
elemType tmp = move(i)->data;
Node *p = head->next;
int j = 0;
while (p) {
if (p->data == tmp) {
p = p->next;
remove(j);
} else {
p = p->next;
++j;
}
}
}
sLinkList<char> *text[105] = {0};
int lines = 0;
void list1(int, int);
void ins(int, int, char *);
void del(int, int, int);
int main() {
text[0] = new sLinkList<char>();
char in;
char order[10];
char str[150];
int x, y, i;
cin.get(in);
while (in != '*') {
if (in == '\n') {
text[++lines] = new sLinkList<char>();
} else {
text[lines]->push(in);
}
cin.get(in);
}
while (in != '\n') {
cin.get(in);
}
while (1) {
cin >> order;
if (strcmp(order, "quit") == 0) {
break;
} else if (strcmp(order, "list") == 0) {
cin >> x >> y;
list1(x, y);
} else if (strcmp(order, "ins") == 0) {
cin >> x >> y;
i = 0;
cin.get(in);
while (1) {
cin.get(in);
if (in == '\n') break;
str[i++] = in;
}
str[i] = 0;
ins(x, y, str);
} else if (strcmp(order, "del") == 0) {
cin >> x >> y >> i;
del(x, y, i);
} else {
cout << "Error!" << endl;
}
}
for (int i = 0; i < lines; ++i) {
text[i]->traverse();
if (i != lines - 1) {
cout << endl;
}
}
return 0;
}
void list1(int n1, int n2) {
if (n1 < 1 || n2 < 1 || n1 > n2 || n1 > lines || n2 > lines) {
cout << "Error!" << endl;
} else {
while (n1 <= n2) {
text[n1 - 1]->traverse();
cout << endl;
++n1;
}
}
}
void ins(int i, int j, char *str) {
if (i < 1 || j < 1 || i > lines || j > text[i - 1]->length() + 1) {
cout << "Error!" << endl;
} else {
int ind = 0;
while (str[ind]) {
text[i - 1]->insert(j - 1 + ind, str[ind]);
++ind;
}
}
}
void del(int i, int j, int num) {
if (i < 1 || j < 1 || i > lines || j + num > text[i - 1]->length() + 1) {
cout << "Error!" << endl;
return;
} else {
while (num--) {
text[i - 1]->remove(j - 1);
}
}
}
skyzh's solution
#include <iostream>
#include <string>
using namespace std;
#define err cout << "Error!" << endl
string text[10000];
int main() {
int R = 0;
while (true) {
getline(cin, text[R++]);
if (text[R - 1] == "******") {
--R;
break;
}
}
while (true) {
string cmd;
cin >> cmd;
if (cmd == "quit") {
for (int i = 0; i < R; i++) cout << text[i] << endl;
break;
}
if (cmd == "list") {
int op1, op2;
cin >> op1 >> op2;
if (op1 >= 1 && op2 <= R && op1 <= R && op2 >= 1 && op1 <= op2)
for (int i = op1; i <= op2; i++) cout << text[i - 1] << endl;
else err;
}
if (cmd == "ins") {
int i, j;
string str;
cin >> i >> j;
getline(cin, str);
str = str.substr(1);
if (i >= 1 && i <= R && j >= 1) {
if (j - 1 <= text[i - 1].length()) {
text[i - 1].insert(j - 1, str);
} else err;
} else err;
}
if (cmd == "del") {
int i, j, num;
cin >> i >> j >> num;
if (i >= 1 && i <= R && j >= 1 && num >= 0) {
if (j + num - 1 <= text[i - 1].length()) {
text[i - 1].erase(j - 1, num);
} else err;
} else err;
}
}
return 0;
}
yyong119's solution
#include<iostream>
#include<cstring>
using namespace std;
char text[110][2010], c, Tmp[110], u[2010];
int num1 = 0, num2 = 0, num3 = 0, order_flag = 1, L = 0;
// int strcmp(const char* str1, const char* str2) {
// int ret = 0;
// while (!(ret = *(unsigned char*)str1 - * (unsigned char*)str2) && *str1) {
// str1++;
// str2++
// }
// if (ret < 0)
// return -1;
// else if (ret > 0)
// return 1;
// return 0;
// }
void quit() {
for (int i = 0; i < L; i++) cout << text[i] << endl;
}
void list(int num1, int num2) {
for (int i = (num1 - 1); i <= (num2 - 1); i++) cout << text[i] << endl;
}
void ins(int num1, int num2, char *tmp) {
for (int i = (strlen(text[num1 - 1]) - 1); i >= (num2 - 1); i--)
text[num1 - 1][i + strlen(tmp)] = text[num1 - 1][i];
for (int i = 0; i < strlen(tmp); i++)
text[num1 - 1][i + num2 - 1] = tmp[i];
}
void del(int num1, int num2, int num3) {
int R = strlen(text[num1 - 1]);
for (int i = 0; i < (strlen(text[num1 - 1]) - num3 - num2 + 1); i++)
text[num1 - 1][i + num2 - 1] = text[num1 - 1][i + num3 + num2 - 1];
for (int i = 0; i < num3; i++)
text[num1 - 1][R - 1 - i] = NULL;
}
int main() {
for (int i = 0; i < 110; i++) {
cin.getline(text[i], 2000);
L++;
if (strcmp(text[i], "******") == 0) {
L--;
break;
}
}
while (true) {
cin >> Tmp;
if (strcmp(Tmp, "list") == 0) {
cin >> num1 >> num2;
if (num1 > num2 || num1 < 1 || num2 < 1 || num2 > L || num1 > L)
cout << "Error!" << endl;
else
list(num1, num2);
}
else if (strcmp(Tmp, "ins") == 0) {
char tmp[100];
cin >> num1 >> num2;
c = cin.get(); cin.getline(tmp, 100);
if (num1 < 1 || num2 < 1 || num1 > L || (num2 > (strlen(text[num1 - 1]) + 1)))
cout << "Error!" << endl;
else
ins(num1, num2, tmp);
}
else if (strcmp(Tmp, "del") == 0) {
cin >> num1 >> num2 >> num3;
if (num1 < 1 || num2 < 1 || num1 > L || num2 > strlen(text[num1 - 1]) || (num2 + num3) > (strlen(text[num1 - 1]) + 1))
cout << "Error!" << endl;
else
del(num1, num2, num3);
}
else if (strcmp(Tmp, "quit") == 0)
break;
}
quit();
return 0;
}