主题:Toj1020字符串编辑WHY Wrong Answer?
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
Source
noip96普及组
var
n,i,j:longint;
error:boolean;
s,que:string;
begin
readln(n);
for j:=1 to n do
begin
readln(s);
readln(que);
case que[1] of
'D':begin i:=pos(que[3],s);
if i=0
then begin writeln('error');
halt;end
else delete(s,i,1);
end;
'I':begin i:=length(s);
while (i>0)and(s[i]<>que[3]) do
i:=i-1;
if i<>0
then insert(que[5],s,i)
else begin writeln('error');
halt;end;
end;
'R':begin for i:=1 to length(s)do
if s[i]=que[3]
then s[i]:=que[5]
end;
end;
writeln(s);
end;
end.
以上是本人的CODE,不知怎么就是过不了,请大牛们指导!!!!
从键盘输入一个字符串(长度<=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
Source
noip96普及组
var
n,i,j:longint;
error:boolean;
s,que:string;
begin
readln(n);
for j:=1 to n do
begin
readln(s);
readln(que);
case que[1] of
'D':begin i:=pos(que[3],s);
if i=0
then begin writeln('error');
halt;end
else delete(s,i,1);
end;
'I':begin i:=length(s);
while (i>0)and(s[i]<>que[3]) do
i:=i-1;
if i<>0
then insert(que[5],s,i)
else begin writeln('error');
halt;end;
end;
'R':begin for i:=1 to length(s)do
if s[i]=que[3]
then s[i]:=que[5]
end;
end;
writeln(s);
end;
end.
以上是本人的CODE,不知怎么就是过不了,请大牛们指导!!!!