关于华为机试题求代码!!!

lanmeng521131485 2012-04-18 09:47:26
n个字符串,1<n<20;找出n个字符串中相同的最长的子字符串;
如n=3
1.what is local bus?
2.this is local bus。
3.local bus is name sdhfj。
那么最长的共同子串是local bus
函数为char * findchar(const char**asd,const int n)
大体就是这些,用c去实现!!
大牛们 给力啊!!
...全文
4847 67 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
67 条回复
切换为时间正序
请发表友善的回复…
发表回复
九月天晴 2014-03-25
  • 打赏
  • 举报
回复
不知道啊,,,怎么做
txzsp 2012-05-02
  • 打赏
  • 举报
回复
用双重循环
zff869030831 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 62 楼 的回复:]

61楼的解法是子序列,和子串是两码事。
较好的做法是:
将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,
求后缀数组。这个做法的时间复杂度为O(nlogn)。
至于为什么,可以去学后缀数组,学了后缀数组就会发现这是裸题,没学的话看代码也看得蛋疼
[/Quote]
62L的解法太棒了,效率又高,用后缀数组又好实现。非常好!
lanmeng521131485 2012-04-24
  • 打赏
  • 举报
回复
[Quote=引用 62 楼 的回复:]
61楼的解法是子序列,和子串是两码事。
较好的做法是:
将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,
求后缀数组。这个做法的时间复杂度为O(nlogn)。
至于为什么,可以去学后缀数组,学了后缀数组就会发现这是裸题,没学的话看代码也看得蛋疼
[/Quote]
哈哈 谢谢在研究中!!
DeDeWo 2012-04-24
  • 打赏
  • 举报
回复
61楼的解法是子序列,和子串是两码事。
较好的做法是:
将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,
求后缀数组。这个做法的时间复杂度为O(nlogn)。
至于为什么,可以去学后缀数组,学了后缀数组就会发现这是裸题,没学的话看代码也看得蛋疼
z415904376 2012-04-22
  • 打赏
  • 举报
回复
lz 把效率最好的代码帖出来呗,
lanmeng521131485 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 54 楼 的回复:]
lz 把效率最好的代码帖出来呗,
[/Quote]
已贴 请指导!
lanmeng521131485 2012-04-22
  • 打赏
  • 举报
回复
关于这个代码我不确定是最有效率的,但是是最符合我的要求的。本代码由mxs456提供。在此表示十分感谢。
关于具体的思路大家都可以想到,就是实现的问题。
由于本帖就要结贴了,在此感谢楼上热心回帖的童鞋们!



#include <stdio.h>
#include <string.h>

char *findchar(const char **ch, const int n);
int isfind(const char **ch, const int n, char *needfindchar);
char curr[100]={0};//在这里进行了初始化要不然在第一次用到strlen的时候面临着任意数
char maxchar[100]={0};
//"this is local bus"

int main(int argv, char ** argc){
const char *ch[] = {
"what is local busxz12345678901xx",
" is local bus zz12345678901xx",
"local bus is name sdhfjzz12345678901xx"
};
printf("the same is=%s!\n",findchar(ch, 3));
return 1;
}

char *findchar(const char **ch, const int n){
int shornum = 0;
int i;
for(i=0; i< n-1; i++){
//printf("%s\n",ch[i]); 找到n个字符串中最短的
if(strlen(ch[i]) > strlen(ch[i + 1])){
shornum = i+1;
}
}
//printf("%s\n",ch[shornum]);
int len=strlen(ch[shornum]);
int charIndex=0;//第一次以字符查找到的位置
int isfindchar=0;//是否找到
for(i=0; i<len; i++){
if(isfindchar){
memset(curr,'\0',strlen(curr));
strncpy(curr, ch[shornum]+charIndex, i - charIndex + 1);
printf("111=%s,i=%d\n",curr,i);
isfindchar=isfind(ch,n, curr);
if(!isfindchar){//如果没有找到,则需要从没有找到的串的位置重新开始查找
printf("no--\n");
i = i - 1;
if((strlen(curr) - 1) > strlen(maxchar)){
memset(maxchar,'\0',strlen(maxchar));
strncpy(maxchar, ch[shornum]+charIndex, i - charIndex+1);//在这里改成加1。
}
}
if(i==len -1 ){
if((strlen(curr) - 1) > strlen(maxchar)){
memset(maxchar,'\0',strlen(maxchar));
strncpy(maxchar, ch[shornum]+charIndex, i - charIndex + 1);
}
}
}
else
{
memset(curr,'\0',strlen(curr));
strncpy(curr, ch[shornum] + i, 1);
printf("222=%s,i=%d\n",curr,i);
isfindchar=isfind(ch,n, curr);
charIndex = i;
}
}
return maxchar;
}

int isfind(const char **ch, const int n, char *needfindchar){
int isfind = 0;
int i;
for(i=0; i<n; i++){
//printf("---%s\n",ch[i]);
if(strstr(ch[i], needfindchar)) isfind=1;
else{
return 0;
}
}
return isfind;
}
wwwyanghaiteng 2012-04-22
  • 打赏
  • 举报
回复
这个算法效率有点低,但暂时只能想出这个了……

#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;
}
wwwyanghaiteng 2012-04-22
  • 打赏
  • 举报
回复
发现好多人比较两个字符串是都只考虑找最长的子序列。但是假如两个字符串中存在好几个一样长的都是最长的子序列呢?
另外题目给出的函数原型char * findchar(const char**asd,const int n)
要求返回一个字符串指针,这个内存不能由系统栈分配,又要自己进行堆内存分配的管理;
其次是这个const char**asd, lz没看错么?应该是char** const asd吧?
会思考的草 2012-04-22
  • 打赏
  • 举报
回复
这道题有很多解法了,DP是效率最高的。
随便找一个:
http://blog.csdn.net/chenwenshi/article/details/6027884
会思考的草 2012-04-22
  • 打赏
  • 举报
回复
这不是一个很经典的最长公共子串的问题么,用动态规划。
qiuhui00 2012-04-22
  • 打赏
  • 举报
回复
我看我还是不说我的解法了,虽然可以实现,但是效率不高,掩面飘过
懒散的笔记 2012-04-21
  • 打赏
  • 举报
回复
估计你还是个学生吧,人家给出了要求的函数原型,要求的是入参是个二维指针,这个二维指针是要求传进N个字符串的,要是你去机试直接过不了。。。更不要说你的实现效率很低。。。
[Quote=引用 21 楼 的回复:]

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)……
[/Quote]
lanmeng521131485 2012-04-21
  • 打赏
  • 举报
回复
准备结贴了 我已经各道代码了!!
半个馒头 2012-04-21
  • 打赏
  • 举报
回复
要是可以用perl的话,直接blast就得了,呵呵
snhnbb 2012-04-20
  • 打赏
  • 举报
回复
还有参考:算法导论 中文第二版 第208页 15.4节 最长公共子序列。
关注习题15.4-6 是优化后的O(nlgn)算法。
SurgePing 2012-04-20
  • 打赏
  • 举报
回复
+1
[Quote=引用 19 楼 的回复:]

引用 16 楼 的回复:

引用 12 楼 的回复:

引用 8 楼 的回复:

先两个字符串找 公共子串 。
然后拿公共子串和下一个字符串找 公共子串
往复循环,最后剩下的 公共子串 中找最长的 子串 返回。
最后没有剩下公共子串则返回NULL。

个人想法,估计这个贴会火,来顶一个..

顶,只要写出针对两个串求最长公共串的代码,剩下就是循环的问题了,当然,可……
[/Quote]
snhnbb 2012-04-20
  • 打赏
  • 举报
回复
这个是最长公共子序列问题,一般使用动态规划处理。
汗~面试一般也就求个2个序列最长的吧。

参考:http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
中文的:http://zh.wikipedia.org/wiki/%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%AD%90%E5%BA%8F%E5%88%97
sepcity 2012-04-20
  • 打赏
  • 举报
回复
这个不是最长公共子串问题么- -
加载更多回复(39)

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧