求两个串中的第一个最长子串
来源:互联网 发布:c语言中volatile byte 编辑:程序博客网 时间:2024/04/30 04:29
题目:求两个串中的第一个最长子串(神州数码以前试题)。如"abractyeyt","dgdsaeactyey"的最大子串为"actyey"。
#include <iostream>
#include <string>using namespace std;
//两个串中的第一个最长子串
string FindLongestCommonSubString(string strOne, string strTwo)
{
if ("" == strOne || strOne.length() <= 0 || "" == strTwo || strTwo.length() <= 0)
{
return NULL;
}
if (strOne.length() < strTwo.length()) //strOne始终是最长的, 相当于母串,strTwo相当于子串
{
string strTmp = strOne;
strOne = strTwo;
strTwo = strTmp;
}
int *mask = new int[strOne.length()];
int maxlength = 0;
int end = -1;
memset(mask, 0, sizeof(int)*strOne.length());
for (int i = 0; i < strTwo.length(); i++)
{
for (int j = strOne.length() - 1; j >= 0; j--)
{
if (strTwo[i] == strOne[j])
{
if (i == 0 || j == 0)
{
mask[j] = 1; //母串为第一个元素,或者子串只有一个元素
}
else
{
mask[j] = mask[j - 1] + 1;
}
}
else
{
mask[j] = 0;
}
if (mask[j] > maxlength)
{
maxlength = mask[j];
end = j;
}
}
}
delete [] mask;
return strOne.substr(end - maxlength + 1, maxlength);
}
int main()
{
string strOne = "abractyeyt";
string strTwo = "dgdsaeactyey";
cout<<"第一个字符串: "<<strOne<<endl;
cout<<"第二个字符串: "<<strTwo<<endl;
cout<<"最长公共子串: "<<FindLongestCommonSubString(strOne, strTwo);
cout<<endl;
return 0;
}
- 求两个串中的第一个最长子串
- 求两个串中的第一个最长子串
- 求两个串中的第一个最长子串,如"abractyeyt"和"dgdsaeactyey"的第一个最长字串是"actyey"
- 求两个串中的第一个最长子串(神州数码以前试题)。
- Suffix Trees 求两个串中的第一个最长子串
- 数据结构——算法之(032)(求两个串中的第一个最长子串)
- 求两个串中第一个最长的子串 神州数码
- 两个字符串的第一个最长公共子串
- 求两个串中的一个最长子串
- 求两个字符串中的最长公共子串的长度
- 求两个字符串a,b中的最长公共子串
- 求两个字符串最长公共子串
- 第一个最长的回文子串
- 求串S中出现的第一个最长重复子串及其位置。
- 求一个串中出现的第一个最长重复子串
- 求一个串中出现的第一个最长重复子串
- 求串的第一个最长重复子串的下标和长度
- (笔试前复习)java-两种方法求第一个最长的可重复子串
- spring security3.x学习(16)_JdbcUserDetailManager的使用
- 黑马程序员_正则表达式
- mybatis generator 配置文件解析
- cocos2d-x 场景切换时由资源预加载问题引起的场景无法切换的问题
- Code Jam 2010 Round 1B Problem A
- 求两个串中的第一个最长子串
- 阿里研发一笔试题foo(2^31-3)
- 2014年创新工场校园招聘笔试题及答案
- Linux下几个常用的快捷键
- 王菲离婚后首发微博谈及与李亚鹏离婚原因
- 实现tabView 一个页面内有有多个子页
- 先序中序非递归 图的深度遍历
- hdu 2844 DP
- ME722 刷机