# 关于华为机试题求代码！该怎么处理(3)

www.MyException.Cn  网友分享于：2013-04-24  浏览：41次

continue;
else
break;
}
if(k == N)//说明子串tmpStr是其他参与匹配的子串的子串
{
if(tmpStr.length() > maxlen)//tmpStr如果是当前最大的子串，则记录下来
{
maxlen = tmpStr.length();
maxStr = tmpStr;
}
}
}
}
cout << "最大公共子串为：";
cout << maxStr <<endl;
delete []pStr;
return 0;
}

C/C++ code

#include <iostream>
#include <string>

using namespace std;

//将第一个字符串与最短的字符串交换
void swap(string *pStr,int i)
{
string temp;
temp = *pStr;
*pStr = *(pStr + i);
……

------解决方案--------------------

Longest Common Subsequence

Challenge Description
You are given two sequences. Write a program to determine the longest common subsequence between the two strings(each string can have a maximum length of 50 characters). (NOTE: This subsequence need not be contiguous. The input file may contain empty lines, these need to be ignored.)

Input
The first argument will be a file that contains two strings per line, semicolon delimited. You can assume that there is only one unique subsequence per test case. e.g.

XMJYAUZ;MZJAWXU

Output
The longest common subsequence. Ensure that there are no trailing empty spaces on each line you print. e.g.

MJAU

------解决方案--------------------

------解决方案--------------------
1.通过二维矩阵找出任意两个字符串的所有公共子串

2.将找出的所有公共子串一一跟剩下的字符串匹配，看看其中有没有。
------解决方案--------------------

------解决方案--------------------

------解决方案--------------------

C/C++ code
#include <iostream>
#include <string>

using namespace std;

//将第一个字符串与最短的字符串交换
void swap(string *pStr,int i)
{
string temp;
temp = *pStr;
*pStr = *(pStr + i);
*(pStr + i)……

------解决方案--------------------

------解决方案--------------------

C/C++ code
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void find(const string &str1, const string &str2,  vector<string> &result)
{
typedef vector<string>::size_type size_t;
typedef vector<string>::difference_type diff_t;

result.clear();
size_t length = 0;
const size_t length1=str1.length(),
length2=str2.length();

for (size_t pos1=0; length1!=pos1; ++pos1) {
for (size_t pos2=0; length2!=pos2; ++pos2) {
diff_t diff=0;
for (;
pos1+diff != length1 &&
pos2+diff != length2 &&
str1[pos1+diff] == str2[pos2+diff];
++diff) ;
if (length < diff) {
length = diff;
result.clear();
result.push_back( str1.substr(pos1,diff) );
} else if (length == diff) {
length = diff;
result.push_back( str1.substr(pos1,diff) );
}
}
}
}

void findchar (char ** const asd, const size_t n)
{
if (n<1) return;
if (n<2) {
cout << asd[0] << endl;
return;
}

vector<string> result;
find(asd[0], asd[1], result);

typedef vector<string>::const_iterator const_iter;
for (size_t i=2; i!=n; ++i) {
vector<string> list(result);
for (const_iter beg=list.begin(), end=list.end();
beg != end;
++beg) {
find(asd[i],*beg, result);
}
}

for (const_iter beg=result.begin(), end=result.end();
beg != end;
++beg) {
cout << *beg << endl;
}
}

int main()
{
char *asd[3];
char result[256];
char *str0="what is local bus?";
char *str1="this is local bus.";
char *str2="local bus is name sdhfj.";
asd[0] = str0;
asd[1] = str1;
asd[2] = str2;
findchar (asd, 3);
return 0;
}```