有没有大神可以帮忙看一下

wenwen121313 2014-09-16 08:43:52
今天在九度上面做一个题目
题目描述:
Find a longest common subsequence of two strings.
输入:
First and second line of each input case contain two strings of lowercase character a…z. There are no spaces before, inside or after the strings. Lengths of strings do not exceed 100.
输出:
For each case, output k – the length of a longest common subsequence in one line.
样例输入:
abcd
cxbydz
样例输出:
2

#include<iostream>
#include<string>
using namespace std;
int max(int m,int n)
{
return m>n?m:n;
}

int main()
{
string str1,str2;
cin>>str1>>str2;
int m,n;
m=str1.length();
n=str2.length();
int a[100][100];
for(int i=0;i<100;i++)
for(int j=0;j<100;j++)
a[i][j]=0;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
if(str1[i-1]!=str2[j-1])
a[i][j]=max(a[i-1][j],a[i][j-1]);
else
a[i][j]=a[i-1][j-1]+1;
}
cout<<a[m][n];
return 0;
}
在自己电脑上都没问题(vs)
但是在九度上面却提示答案错误。有没有大神可以帮忙看一下啊
...全文
117 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenwen121313 2014-11-27
  • 打赏
  • 举报
回复
好久没来看,忘了,非常感谢楼上各位大神的指点
赵4老师 2014-09-17
  • 打赏
  • 举报
回复
//最长公共子序列字符个数
//c[i][j]保存字符串 {xi},{yj},(长度分别为i,j)的最长公共子序列的字符个数
//i=0或者是j=0 时,c[i][j]必定为零, i,j>=0 且 xi=yj, c[i][j]=c[i-1][j-1]+1
//若 i,j>0 但xi!=yj, c[i][j]=max{ c[i-1][j] , c[i][j-1] }
#include <stdio.h>
#include <string.h>
int c[1001][1001];
void lcs(int a, int b, char x[], char y[], int c[][1001]) {
    int i,j;
    for(i=1;i<a;i++)
        c[i][0]=0; //if b==0, set c[i][j]=0;
    for(i=1;i<b;i++)
        c[0][i]=0; // if a==0; set c[i][j]=0;
    for(i=1;i<=a;i++) // if a!=0,b!=0 loop
        for(j=1;j<=b;j++) {
            if(x[i-1]==y[j-1])
                c[i][j]=c[i-1][j-1]+1;
            else if (c[i-1][j]>=c[i][j-1])
                c[i][j]=c[i-1][j];
            else
                c[i][j]=c[i][j-1];
        }
}
int main() {
    char x[1001],y[1001];
    while ( scanf("%s%s",x,y)!=EOF ) {
        int a=strlen(x);
        int b=strlen(y);
        memset(c,0,sizeof(c));
        lcs(a,b,x,y,c);
        printf("%d\n",c[a][b]);
    }
    return 0;
}
nextseconds 2014-09-17
  • 打赏
  • 举报
回复
数组越界考虑要注意,设计的时候最好预留长度
aa5566f4 2014-09-16
  • 打赏
  • 举报
回复
英文看不懂......
brookmill 2014-09-16
  • 打赏
  • 举报
回复
玩这种题目,最好给自己留一点余量。比如这个题,如果是我来写就会定义a[128][128]
brookmill 2014-09-16
  • 打赏
  • 举报
回复
题目说了,Lengths of strings do not exceed 100 输入两个长度100的字符串,a[m][n]数组越界。 所有的100都改成101就好了。
Kang_Fresh 2014-09-16
  • 打赏
  • 举报
回复
题目意思理解错了吧,你百度一下longest common subsequence。 不可能这么简单就实现了。
wenwen121313 2014-09-16
  • 打赏
  • 举报
回复
没人吗。。。

64,647

社区成员

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

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