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

来源:互联网 发布:想在淘宝直播 编辑:程序博客网 时间:2024/04/30 05:32
#include <iostream>#include <string>#include <stdlib.h>using namespace std;int getCommonStrLength(char * pFirstStr, char * pSecondStr){    int i = 0, j = 0, k = 0;    int count = 0, temp = 0;    char *p1 = NULL, *p2 = NULL;    p1 = pFirstStr;    p2 = pSecondStr;    /* 先把字符串转换为小写字母 */    for(i = 0; p1[i] != '\0'; i++)    {        if(p1[i] >= 'A' && p1[i] <= 'Z')        {            p1[i] += 32;        }    }    for(i = 0; p2[i] != '\0'; i++)    {        if(p2[i] >= 'A' && p2[i] <= 'Z')        {            p2[i] += 32;        }    }    for(i = 0; p2[i] != '\0'; i++)    {        for(j = 0; p1[j] != '\0'; j++)        {            if(p2[i] == p1[j])            {                temp = 0;                k = i;                while(p2[k] != '\0' && p1[j] != '\0')                {                    temp++;                    k++;                    j++;                    if(p2[k] != p1[j] || (p2[k] == '\0' || p1[j] == '\0'))                    {                        if(temp > count)                        {                            count = temp;                        }                        break;                    }                }            }        }    }    return count;}int main(){    char str1[30] = {0},str2[30] = {0};    while(cin >> str1 >> str2)    {        cout << getCommonStrLength(str1,str2) << endl;    }    return 0;}
1 0
原创粉丝点击