最大的对称子串
来源:互联网 发布:两小无猜 网络剧 编辑:程序博客网 时间:2024/06/11 00:49
1.输入字符串中对称的子字符串的最大长度。比如输入字符串“roorle”,由于该字符串里最长的对称子字符串是“roor”,因此输出4.
(1)用代码实现方法;
(2)设计并写出测试用例,测试自己所实现的方法;
(3)请给出编程时容易出现的bug现象,原因;
(4)有无其他实现方法(说明思路即可),比较这两种实现方法优劣,以及各自容易产生的bug有什么不同?
1)最长对称子串可能是偶数个字符,也可能是奇数个字符,分别进行判断。当判断的时候,以单个字符或者两个字符为中心,向左右两侧延伸,判断其左右两侧的字符是否相同,时间复杂度O(n^2).
int getMaxSubstring(char* pstr){ if(pstr==NULL) { return -1; } if(pstr==""||pstr[1]=='\0') { return 1; } int maxlen = 1; char* pchar = pstr + 1; while(*pchar != '\0') { //子串为奇数个字符 char *pleft = pchar - 1; char *pright = pchar + 1; while(pleft >= pstr && pright <= &pstr[strlen(pstr) - 1] && *pleft == *pright) { pleft--; pright++; } int templen = pright - pleft - 1; if(templen > maxlen) { maxlen = templen; } //子串为偶数个字符 pleft = pchar - 1; pright = pchar; while(pleft >= pstr && pright <= &pstr[strlen(pstr) - 1] && *pleft == *pright) { pleft--; pright++; } templen = pright - pleft - 1; if(templen > maxlen) { maxlen = templen; } pchar++; } return maxlen;}
2)测试用例
NULL
“”
a
google
121
aabbaa
111222aabbaa
测试结果正确,如下:
3) 编程时可能出现的bug有:
a. 没有对字符串为NULL的情况进行判断
b. 最长对称子串可能是偶数个字符也可能是奇数个字符,编程的时候可能考虑不全面
c. pleft >= pstr 和 pright <= &pstr[strlen(pstr) - 1] 没有添加等号,没有判断边界情况
4) 上述算法的时间复杂度为O(n^2),也可以对字符串的每一个子串进行判断,遍历字符串的每一个字符,然后从该字符开始依次向后取以该字符开始的每一个子串,判断该子串是否为对称子串,然后取最长的对称子串即可,这种方法时间复杂度高,为O(n^3)。
完整代码如下:
//dui.cpp
#include <iostream>using namespace std;int getMaxSubstring(char* pstr){ if(pstr==NULL) { return -1; } if(pstr==""||pstr[1]=='\0') { return 1; } int maxlen = 1; char* pchar = pstr + 1; while(*pchar != '\0') { //子串为奇数个字符 char *pleft = pchar - 1; char *pright = pchar + 1; while(pleft >= pstr && pright <= &pstr[strlen(pstr) - 1] && *pleft == *pright) { pleft--; pright++; } int templen = pright - pleft - 1; if(templen > maxlen) { maxlen = templen; } //子串为偶数个字符 pleft = pchar - 1; pright = pchar; while(pleft >= pstr && pright <= &pstr[strlen(pstr) - 1] && *pleft == *pright) { pleft--; pright++; } templen = pright - pleft - 1; if(templen > maxlen) { maxlen = templen; } pchar++; } return maxlen;}void main(){ //测试用例7个 int a=-3,b=-3,c=-3,d=-3,e=-3,f=-3,g=-3; //1.字符串为NULL a=getMaxSubstring(NULL); if (a==-1) { printf("error\n"); } //2.字符串为空 b=getMaxSubstring(""); if (b==1) { printf("error\n"); } //3.字符串只有一个元素 c=getMaxSubstring("a"); if (c!=0) { printf("%d\n",c); } //4.英文字符串 d=getMaxSubstring("google"); if (d!=0) { printf("%d\n",d); } //5.数字字符串 e=getMaxSubstring("121"); if (e!=0) { printf("%d\n",e); } //6.全对称字符串 f=getMaxSubstring("aabbaa"); if (f!=0) { printf("%d\n",f); } //7.数字与英文混合的字符串 g=getMaxSubstring("111222aabbaa"); if (g!=0) { printf("%d\n",g); }}
0 0
- 最大的对称子串
- 对称子串的最大长度
- 字符串中对称子串的最大长度
- 字符串中对称子串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 求对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- celery安装测试
- sql中where 1=1 和where 0=1的作用
- myBatis抛出异常Result Maps collection already contains value of xxx
- 构建方法返回String的报错问题
- 变化
- 最大的对称子串
- 从xcode7回到6.4之后的(null): Linker command failed with exit code 1 (use -v to see invocation)问题
- Unity5.2.0无法使用Visual Studio 2013调试问题解决方案
- MQ安装
- 黑马程序员——Java基础---集合
- tf卡恢复工具
- 一只简单的网络爬虫(基于linux C/C++)————配置文件设计及读取
- Working with Apple’s App Transport Security
- Masonry介绍与使用实践(快速上手Autolayout)