一道很简单的题,用c++ 就是通不过。

hqycxy 2005-05-06 09:49:06
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 上提交就是通不过,有人帮看下么?谢谢了。
...全文
233 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jp1984 2005-05-07
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

#define SIZE 40

/*参考*/

/* ----------------------------------------- */
char* erase(char*,char);
char* insert(char*,char,char);
char* replace(char*,char,char);
void command(char*,char*);
/* ----------------------------------------- */

void command(char* s,char* source)
{

if(s[0] == 'D')

erase(source,s[2]);/* formation : D x */

else if(s[0] == 'I')

insert(source,s[2],s[4]);

else if(s[0] == 'R')
replace(source,s[2],s[4]);


}

char* erase(char* s,char which)
{
int i = 0,j;
for(;i < strlen(s);i++){
if(s[i] == which){
for(j = i;j < strlen(s);j++)
s[j] = s[j + 1]; /* erase it */
break;
}
}
return s;
}
char* insert(char* s,char x,char y)
{

int i = strlen(s) - 1,j;
for(;i > -1;i--)
if(s[i] == x){
for(j = strlen(s);j >= i;j--)
s[j + 1] = s[j];
s[i] = y;
break;
}

return s;
}
char* replace(char* s,char x,char y)
{
int i = 0;
for(;i < strlen(s);i++){
if(s[i] == x)
s[i] = y; /* relpace it */
}
return s;
}
int main(int argc, char *argv[])
{
int cnt = 0,in,i = 0;
char com[10],str[SIZE];

while((in = getchar()) != '.'){
str[cnt] = in;
cnt++;
}

str[cnt] = '\0';

printf("Input command (ctrl + Z to end): \n");

com[0] = getchar();
while((in = getchar()) != EOF){
com[i] = in;
i++;
}
com[i] = '\0';

command(com,str);

printf("%s\n",str);

system("PAUSE");
return 0;
}
wasltone 2005-05-07
  • 打赏
  • 举报
回复
对了,==不是像你这样函数转太多了,一个一次性Switch控制,OK?
而且你是WA,还是TLE,C++的时间可能会超时
wasltone 2005-05-07
  • 打赏
  • 举报
回复
Tju的题目吧?
用一个switch控制,如果输入'I' 或'D' 等,分别处理,这样就简单了
flyingdancing2005 2005-05-06
  • 打赏
  • 举报
回复
学习中...

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧