华为OJ

ID:2011 

公共字串计算       
描述: 

题目标题:

计算两个字符串的最大公共字串的长度,字符不区分大小写

详细描述:

接口说明

原型:

int getCommonStrLength(char * pFirstStr, char * pSecondStr);

输入参数:

     char * pFirstStr //第一个字符串

     char * pSecondStr//第二个字符串

 

 
知识点:  字符串,查找 
题目来源:  内部整理 
练习阶段:  初级 
运行时间限制: 10Sec
内存限制: 128MByte
输入:  

输入两个字符串

 
输出:  

输出一个整数

 
样例输入:
asdfas werasdfaswer
                   
样例输出:
6
                    


[code=c]#include <string>
#include <iostream>
#include <stdlib.h>

using namespace std;

int getCommonStrLength(char * pFirstStr, char * pSecondStr)
{
	int len1 = strlen(pFirstStr);
	int len2 = strlen(pSecondStr);
	int i;
	int maxValue = 0;
	char *Fstr,*Sstr;
	int len = len1<=len2 ? len1 : len2;
	if(len == len1)
	{
		Fstr = (char *)malloc(sizeof(char)*(len1+1));
		Sstr = (char *)malloc(sizeof(char)*(len2+1));
		for ( i=0;i<len1;i++)
			Fstr[i] = tolower(pFirstStr[i]);
		Fstr[i] = '\0';
		for( i=0;i<len2;i++)
			Sstr[i] = tolower(pSecondStr[i]);
		Sstr[i] = '\0';
	}
	else
	{
		Fstr = (char *)malloc(sizeof(char)*(len2+1));
		Sstr = (char *)malloc(sizeof(char)*(len1+1));
		for ( i=0;i<len2;i++)
			Fstr[i] = tolower(pSecondStr[i]);
		Fstr[i] = '\0';
		for( i=0;i<len1;i++)
			Sstr[i] = tolower(pFirstStr[i]);
		Sstr[i] = '\0';
	}
//	cout<<"Fstr:"<<Fstr<<endl;
//	cout<<"Sstr:"<<Sstr<<endl;

	for (int m=0;m<len;m++)
	{
		int tmp = m;
		int j = 0;
		while ( j != strlen(Sstr))
		{
			int tmpValue = 0;
			while (Fstr[tmp] != Sstr[j] && j != strlen(Sstr))
				j++;
			if(j == strlen(Sstr))
				break;
			else
			{
				tmpValue++;
				while (Fstr[++tmp] == Sstr[++j] && tmp != strlen(Fstr))
				{
					tmpValue++;
				}		
					if (maxValue < tmpValue)
						maxValue = tmpValue;
					tmp = m;				
			}
		}
	}
	return maxValue;
}

int main()
{
	char s1[200],s2[200];
	cin>>s1>>s2;
//	cout<<"s1:"<<s1<<endl;
//	cout<<"s2:"<<s2<<endl;
	int value = getCommonStrLength(s1,s2);
	cout<<value<<endl;
	return 0;
}
[/code]


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值