找公共子串
2015-09-02 12:31
183 查看
In asdff fffasdfffaaaf
Out 5
寻找公共子串,我的思想就是两层循环,,最短每层的一个i开始最大与长的字符串的共同子串,遍历,暴力搜索。
注意大小写, 和注意思路就好
只有280分,如果有优化代码,请多多指教:
代码如下,
Out 5
寻找公共子串,我的思想就是两层循环,,最短每层的一个i开始最大与长的字符串的共同子串,遍历,暴力搜索。
注意大小写, 和注意思路就好
只有280分,如果有优化代码,请多多指教:
代码如下,
#include <stdio.h> #include <ctype.h> #include <iostream> using namespace std; #include <string> int main() { int getCommonStrLength(char * pFirstStr, char * pSecondStr); char pFirstStr[128]; char pSecondStr[128]; cin>>pFirstStr; cin>>pSecondStr; int a=getCommonStrLength(pFirstStr,pSecondStr); cout<<a<<endl; system("Pause"); return 0; } void mytower(char* strSrc) { while(*strSrc!='\0') { if (*strSrc>='A'&&*strSrc<='Z') { *strSrc=*strSrc+32; } strSrc++; } } int getCommonStrLengthPart(char *pSmall,char *strSecond) { int nCount=0; while(*pSmall==*strSecond&&(*pSmall!='\0'&&strSecond!='\0')) { nCount++; pSmall++; strSecond++; } return nCount; } int getCommonStrLength(char * pFirstStr, char * pSecondStr) { int count=0; int MaxCount=0; mytower(pFirstStr); mytower(pSecondStr); int pNumber1=strlen(pFirstStr); int pNumber2=strlen(pSecondStr); if (pNumber1==0||pNumber2==0) { return 0; } char *pSmall; char *plonger; int SmallNumber,longLength; pNumber1<=pNumber2? pSmall=pFirstStr:pSmall=pSecondStr; pNumber1<=pNumber2? plonger=pSecondStr:plonger=pFirstStr; pNumber1<=pNumber2? (SmallNumber=pNumber1,longLength=pNumber2):(SmallNumber=pNumber2,longLength=pNumber1); //fen bian hou for (int i=0;i<SmallNumber;i++) { for (int j=0;j<longLength;j++) { count=getCommonStrLengthPart(&pSmall[i],&plonger[j]); { if (count>MaxCount) { MaxCount=count; } } } } return MaxCount; }
相关文章推荐
- 微信自动回复代码
- android开发软键盘遮挡登陆按钮
- Java加密技术(五)——非对称加密算法的由来DH
- poj2955 Brackets
- 求解百鸡问题
- Qt中使用信号和槽的一点心得
- [LeetCode#202] Roman to Integer
- set_difference
- Hierarchy
- PHP面向对象之方法重写
- error C2275 将此类型用作表达式非法
- JVM内存JAVA_OPTS参数说明
- php 日志调试代码
- ASIHTTPRequest使用指南---<<翻译稿>>老外的很有价值
- POJ 3984 迷宫问题(BFS or DFS)
- 一小步
- zzuli OJ 1086: ASCII码排序(多实例测试)
- linux里有关管道符的疑惑
- JVM的GC(概念与深入)
- LeetCode Remove Duplicates from Sorted List II