您的位置:首页 > 其它

北邮OJ-107. 字符串操作-11网研上机A

2017-03-20 21:31 369 查看
Debug 记录:

1. 字符串并没有说不包含空格!所以得用gets

2. 由于用了gets,所以得考虑每个样例在用gets之前都得吸收掉前面的回车!!!!!!!这里出bug调了很久!!

3. 题目说的是替代掉“子串”,也即新串的长度不会覆盖掉原串的’\0’字符!所以不能把新串的’\0’写到子串里进去!

题目描述

大家平时都会用到字符串,现在有几种字符串操作,需要你用这几种操作处理下字符串。

HINT

字符串下标从0开始,所有操作的数据范围都合乎规范。

输入格式

多组数据,以EOF结束。

第一行一个字符串,字符串长度大于0,并且小于等于200。

第二行一个数字t,(0< t < =200)。

下面t行,每行表示一种操作。

共有两种操作,每行数据的第一个数表示操作的种类:

翻转操作:第一个是一个数字0,然后两个数字i和len,翻转从下标i长度为len的子串。

替换操作:第一个是一个数字1,然后两个数字i和len,接着一个长度为len的字符串str,用str替换从下标i长度为len的子串。

字符串操作后会更新,旧的字符串被舍弃。(详见sample)

输出格式

每个操作之后输出生成的新的字符串

输入样例

bac

2

0 0 3

1 1 2 as

输出样例

cab

cas

#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXSIZE 500//attention
void swap(char &a,char &b){
char temp=a;
a=b;
b=temp;
}
void reverse(char *str,int p,int len){
for (int i=p,j=i+len-1;i<j;i++,j--){
swap(str[i],str[j]);
}
}
void replace(char *str,int p,int len,char *newStr){
int strLen=strlen(str);
int i;
for (i=0;i<len;i++){/*bug3*/
str[p+i]=newStr[i];
}
//  if (p+len-1>strLen-1)
//      str[p+len]='\0';
}
int main(){
int t;
int operation,p,len;
char str[MAXSIZE],newStr[MAXSIZE];
while (gets(str)){//有没有空格? yes/*bug1*/
//input
scanf("%d",&t);
for (int time=0;time<t;time++){
scanf("%d%d%d",&operation,&p,&len);
switch(operation){
case 0:
getchar();/*bug2*/
reverse(str,p,len);
break;
case 1:
scanf("%s",newStr);
getchar();//attention
replace(str,p,len,newStr);
break;
}
//output
printf("%s\n",str);
}
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: