最大公共字串
2014-07-03 13:29
211 查看
写在前面:小生纯业余选手,开此博仅仅是为了积累,纯当笔记来用。如有看官光临小生博客,请不要相信我的代码就是正确的。如果您发现了错误也恳请耽误您一点时间,请您在下面指出来,不胜感激!
计算两个字符串的最大公共子串,忽略字符串大小写,由于OJ编译器要求,这里变量全部定义在开头。
#include "stdafx.h"
#include<iostream>
#include <string.h>
using namespace std;
int getCommonStrLength(char * ppFirstStr, char * ppSecondStr)
{
int i = 0;
int j = 0;
int len = 0;
int maxlen = 0;
int p = 0;
if (ppFirstStr == NULL || ppSecondStr == NULL)
{
return 0;
}
char *pFirstStr = strlwr(ppFirstStr);
char *pSecondStr = strlwr(ppSecondStr);
while (pFirstStr[i] != '\0')
{
p = i;
while (pSecondStr[j] != '\0' && pSecondStr[j] != pFirstStr[p])
{
j++;
}
if (pSecondStr[j] == '\0')
{
j = 0;
i++;
}else{
while(pFirstStr[p] != '\0' && pSecondStr[j] != '\0' && pFirstStr[p] == pSecondStr[j])
{
len++;
p++;
j++;
}
if (maxlen < len)
{
maxlen = len;
}
j = 0;
i++;
len = 0;
}
}
return maxlen;
}
int _tmain(int argc, _TCHAR* argv[])
{
/*
char *pFirstStr = "asdfas";
char *pSecondStr = "werasdfaswer";
*/
char *pFirstStr = new char[256];
char *pSecondStr = new char[256];
cin>>pFirstStr>>pSecondStr;
cout<<getCommonStrLength(pFirstStr,pSecondStr);
return 0;
}
计算两个字符串的最大公共子串,忽略字符串大小写,由于OJ编译器要求,这里变量全部定义在开头。
#include "stdafx.h"
#include<iostream>
#include <string.h>
using namespace std;
int getCommonStrLength(char * ppFirstStr, char * ppSecondStr)
{
int i = 0;
int j = 0;
int len = 0;
int maxlen = 0;
int p = 0;
if (ppFirstStr == NULL || ppSecondStr == NULL)
{
return 0;
}
char *pFirstStr = strlwr(ppFirstStr);
char *pSecondStr = strlwr(ppSecondStr);
while (pFirstStr[i] != '\0')
{
p = i;
while (pSecondStr[j] != '\0' && pSecondStr[j] != pFirstStr[p])
{
j++;
}
if (pSecondStr[j] == '\0')
{
j = 0;
i++;
}else{
while(pFirstStr[p] != '\0' && pSecondStr[j] != '\0' && pFirstStr[p] == pSecondStr[j])
{
len++;
p++;
j++;
}
if (maxlen < len)
{
maxlen = len;
}
j = 0;
i++;
len = 0;
}
}
return maxlen;
}
int _tmain(int argc, _TCHAR* argv[])
{
/*
char *pFirstStr = "asdfas";
char *pSecondStr = "werasdfaswer";
*/
char *pFirstStr = new char[256];
char *pSecondStr = new char[256];
cin>>pFirstStr>>pSecondStr;
cout<<getCommonStrLength(pFirstStr,pSecondStr);
return 0;
}
相关文章推荐
- 最大公共字串
- 求两个字符串的最大公共字串
- 最大子序列,最长递增子序列,最长公共字串,最长公共子序列,字符串编辑距离
- 动态规划之最长公共子序列和最长公共字串,最大子序列和
- 求两个字符串最大的公共字串的长度
- 【100题】第六十一题~第六十五题(数组中只出现一次的数、链表公共点、删除字串特定字符、寻找丑数、输出从1到最大的N 位数)
- 字符串相关算法(编辑距离,最大公共字串)C#实现版
- 最大公共字串
- 最大公共字串
- 题目标题: 计算两个字符串的最大公共字串的长度,字符不区分大小写
- 求两个字符串的最大公共字串问题
- 两字符串比较 找出最大公共字串 11月18日
- java 温习之字符串(获取两个字符串的最大公共字串)
- 华为机试:计算两个字符串的最大公共字串的长度,字符不区分大小写
- 华为OJ平台 求2个字符串的最大公共字串长度
- 最大公共字串
- 计算两个字符串的最大公共字串的长度,字符不区分大小写(要求计算连续最长字串的长度)
- sdut 1008 最大公共字串
- 最大公共字串求解的简单易懂的方法
- 求两个字符串的最大连续公共字串