一道很简单的题,用c++ 就是通不过。
Problem
从键盘输入一个字符串(长度<=40个字符),并以字符'.'结束.
例如:'This is a book.',现对该字符串进行编辑,编辑功能有:
D:删除一个字符,命令的方式为:
D a 其中a为被删除的字符
例如:D s 表示删除字符's',若字符串中有多个's',则删除第一次出现的,如上例中删除的结果为:
'Thi is a book.'
I:插入一个字符,命令的格式为:
I a1 a2 其中a1表示插入到指定字符前面,a2表示将要插入的字符
例如: I s d 表示在指定字符's'的前面插入字符'd',若原串中有多个's',则插入在最后一个字符的前面,
如上例中,原串:'This is a book.'
插入后:'This ids a book.'
R:替换一个字符,命令格式为:
R a1 a2 其中a1为被替换的字符,a2为替换的字符,若在原串中有多个a1,则应全部替换
例如:原串:'This is a book.'
输入命令: R o e
替换后:' This is a beek.'
Input
该题有多组测试数据,第一行为一个整数N,表示N组测试数据.
接下去N*2行,每组数据包含两行,第一行为原串以'.'结尾,第二行为编辑命令,格式如题目描述。
Output
每组数据输出编辑后的新串,如果未找到指定字符则输出error。
注:当操作为R时,如果未找到指定字符,不属于error。
Sample Input
3
This is a book.
R o e
This is a book.
R t e
This is a book.
D c
Sample Output
This is a beek.
This is a book.
error
========================================
程序:
这是用 c 字符串做的。
#include <iostream>
using namespace std;
int n, len;
char ch[50];
int main()
{
void Make();
Make();
return 0;
}
void Make()
{
void Input();
void CmdR();
void CmdI();
void CmdD();
char cmd;
int i = 0;
cin >> n;
while(i < n) {
Input();
cin.ignore();
cmd = cin.get();
switch(cmd) {
case 'R' : CmdR(); break;
case 'I' : CmdI(); break;
case 'D' : CmdD(); break;
}
i++;
}
}
void Input()
{
char s;
int i = 0;
cin.ignore();
while((s = cin.get()) != EOF) {
ch[i++] = s;
if(s == '.') {
ch[i] = '\0';
len = i;
break;
}
}
}
void CmdR()
{
char d1, d2;
cin.ignore(); d1 = cin.get();
cin.ignore(); d2 = cin.get();
for(int i = 0; i < len; i++)
if(ch[i] == d1)
ch[i] = d2;
cout << ch << endl;
}
void CmdI()
{
char d1, d2;
cin.ignore(); d1 = cin.get();
cin.ignore(); d2 = cin.get();
int yes = 1;
for(int i = len-1; i >= 0; i--)
if(ch[i] == d1) {
for(int j = len; j >= i; j--)
ch[j+1] = ch[j];
ch[i] = d2;
len++;
yes = 0;
break;
}
if(yes)
cout << "error" << endl;
else
cout << ch << endl;
}
void CmdD()
{
char d1;
cin.ignore();
d1 = cin.get();
int yes = 1;
for(int i = 0; i < len; i++)
if(ch[i] == d1) {
for(int j = i; j < len; j++)
ch[j] = ch[j+1];
len--;
yes = 0;
break;
}
if(yes)
cout << "error" << endl;
else
cout << ch << endl;
}
这是用 c++ 字符串做的:
#include <iostream>
#include <string>
using namespace std;
string str, cmd;
int n;
int main()
{
void Input();
void Make();
cin >> n;
cin.ignore(1);
while(n--) {
Input();
Make();
}
//system("pause");
//cout << endl;
return 0;
}
void Input()
{
getline(cin, str, '\n');
getline(cin, cmd, '\n');
}
void Make()
{
char chCmd = cmd[0];
void D();
void I();
void R();
switch(chCmd) {
case 'D' : D(); break;
case 'I' : I(); break;
case 'R' : R(); break;
}
}
void D()
{
char chFinded = cmd[2];
basic_string<char> :: size_type p;
static basic_string<char> :: size_type npos = -1;
p = str.find_first_of(chFinded);
if(p == npos) {
cout << "error" << endl;
return;
}
str.erase(p,1);
if(n == 0) cout << str;
else cout << str << endl;
}
void I()
{
char chFinded = cmd[2], chIns = cmd[4];
basic_string<char> :: size_type p;
static const basic_string<char> :: size_type npos = -1;
p = str.find_last_of(chFinded);
if(p == npos) {
cout << "error" << endl;
return;
}
str.insert(p, 1, chIns);
if(n == 0) cout << str;
else cout << str << endl;
}
void R()
{
char chFinded = cmd[2], chRep = cmd[4];
int p;
while((p = str.rfind(chFinded)) != -1) {
str.replace(p, 1, 1, chRep);
}
if(n == 0) cout << str;
else cout << str << endl;
}
我觉得都没有错了,可是 在 acm.tongji.edu.cn 上提交就是通不过,有人帮看下么?谢谢了。