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]