字符串的问题

guanbenben 2006-08-21 09:16:48
找出两个字符串的最大公串!例如,“adbccadebbca”和“edabccadece”,返回“ccade”
我的运行结果不对,找不到错误,请大家帮助一下.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a[20];
char b[20];
cout<<"please enter string1:"<<endl;
cin>>a;
cout<<"please enter string2:"<<endl;
cin>>b;
int size1=strlen(a);
int size2=strlen(b);
int size=size1>size2?size2:size1;
char *newstring=new char(size+1);
for(int i=0;i<size1;i++)
{
for(int j=0;j<size2;j++)
{
if(a[j]==b[i])
{
int count=0;
while(a[j]!=b[i])
{
j++;
i++;
count++;
}
int max=0;
if(count>max)
{
max=count;
strncpy(newstring,a+j,max);
}
count=0;
}
}
}
cout<<newstring<<endl;
return 0;
...全文
302 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
guanbenben 2006-08-23
  • 打赏
  • 举报
回复
晕,难道是编译器问题吗?我用的6.0,运行输出汉字来了!!!!!!!!
ln_cheng 2006-08-23
  • 打赏
  • 举报
回复
怎么会呢?我都调试出结果了。
ln_cheng 2006-08-22
  • 打赏
  • 举报
回复
char a[20] = "adbccadebbca";
char b[20] = "edabccadece";
//cout<<"please enter string1:"<<endl;
//cin>>a;
//cout<<"please enter string2:"<<endl;
//cin>>b;
int size1=strlen(a);
int size2=strlen(b);
int size=size1>size2?size2:size1;
char *newstring=new char[size+1];
int max=0;
for(int i=0;i<size1;i++)
{
for(int j=0;j<size2;j++)
{
if(a[i]==b[j])
{
int count=0;
int idx = i;
while(a[i]==b[j])
{
j++;
i++;
count++;
}
if(count>max)
{
max=count;
strncpy(newstring,a+idx,max);
}
}
}
}
cout<<newstring<<endl;

delete [] newstring;
jixingzhong 2006-08-22
  • 打赏
  • 举报
回复
int max=0;
这个语句位置不对 ...
jixingzhong 2006-08-22
  • 打赏
  • 举报
回复
char *newstring=new char(size+1);
==》
char *newstring=new char[size+1];
HappyTree 2006-08-22
  • 打赏
  • 举报
回复
#include <iostream>
#include <string.h>

using namespace std;
int main()
{
char a[20];
char b[20];
cout<<"please enter string1:"<<endl;
cin>>a;
cout<<"please enter string2:"<<endl;
cin>>b;
int max=0; // max 应该定义在循环之外,不然每次运算后为0
int size1=strlen(a);
int size2=strlen(b);
int size=size1>size2?size2:size1;
char *pos = NULL;
for(int i=0;i<size1;i++)
{
for(int j=0;j<size2;j++)
{
if(a[j]==b[i])
{
int count=0;
char* pos1 = a+j; // 这里需要保存位置,因为a+j是会变化的
while(a[j]==b[i])
{
j++;
i++;
count++;
}
if(count>max)
{
max = count;
pos = pos1;
}
count = 0;
}
}
}

char temp = *(pos + max);
*(pos + max) = '\0';
cout <<pos<<endl;
*(pos + max) = temp;
return 0;
}
fufangpeng 2006-08-22
  • 打赏
  • 举报
回复
是算法有问题。
for(int i=0;i<size1;i++)
{
for(int j=0;j<size2;j++)//如果字符串1中,在进行比较后,还有与字符串2中相同的字符,则strncpy(newstring,a+idx,max);重写了临时字符串newstring。
{
if(a[i]==b[j])
{
int count=0;
int idx = i;
while(a[i]==b[j])
{
j++;
i++;
count++;
}
if(count>max)
{
max=count;
strncpy(newstring,a+idx,max);
}
}
}
}
具体来说在字符串1中“.....bbca”与字符串2的“..bccadece”存在相同字符,则改写临时字符串。
guanbenben 2006-08-22
  • 打赏
  • 举报
回复
按楼上的改了,还是不出正确结果,看看是不是我的算法有问题???
frankkon 2006-08-21
  • 打赏
  • 举报
回复
在和count比之前,max总是等于0。
liu584 2006-08-21
  • 打赏
  • 举报
回复
google
KMP关键字
就有了

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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