找公共子串

In asdff  fffasdfffaaaf

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值