北邮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
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; }
相关文章推荐
- [置顶] 北邮 oj 107 字符串操作
- 九度oj 题目1105:字符串的反码(string的操作)
- OJ 系列之字符串基本操作
- 107. 字符串操作
- 北邮OJ 90. 字符串转换
- 北邮OJ-88. 最值问题-13网研上机A
- 北邮OJ-266-分数加法-14网研上机A
- 北邮OJ-109. 中序遍历树-11网研上机C
- 九度OJ(字符串、位移操作、背包、递归、dp栈模拟、八皇后)
- 北邮OJ-99. 打印字符串-12网研上机A
- 哈理工OJ 1341 Who will be punished(字符操作,找只出现一次的字符串是哪个)
- 北邮OJ-108. 虚数-11网研上机B
- LeetCode OJ ZigZag Conversion ”之“字符串变化 string操作
- OJ中字符串输入及相关操作
- 【北邮OJ】99. 打印字符串
- 北邮OJ-90. 字符串转换-13网研上机C
- [导入]C中对于字符串的操作总结
- 字符串操作
- 简单的数组操作字符串,加密可用
- C#中一些字符串操作的常用用法