打印

[求助] 编程问题 字符串匹配 总是超时

0

编程问题 字符串匹配 总是超时

求助 字符串匹配 总是超时
题目:字符串匹配  

Time Limit:1000MS Memory Limit:30000KB
Total Submit:333 Accepted:27

Description

给你2个字符串(可能包括数字以及标点),长度<=50124,请你求出最长的连续的公共子序列。

Input

输入有2个字符串A,B, 各占一行。

Output

输出字符串A和B的最长连续公共子序列的长度L。

Sample Input

aaa
aba


Sample Output

1


Source

我的程序:
#include<stdio.h>
#include<string.h>
#define MAX 50124
unsigned int StringCompare(char *a,char *b)
{
     long int la=strlen(a);
     long int lb=strlen(b);
     long int lc=la<lb?la:lb;
     long int i,j;
     long int LMax=0;
     long int Len=0;
     
     for(i=1-la;i<lb;i++)
     {
          Len=0;
          for(j=0;j<la;j++)
          {
               if((i+j>=0)&&(i+j<lb)&&(*(a+j)==*(b+i+j))) Len++;
               else
               {
                    if(Len>LMax) LMax=Len;
                    if(LMax==lc) break;
                    Len=0;
               }
          }
          if(Len>LMax) LMax=Len;
          if(LMax==lc) break;
    }   
    return LMax;
}

int main()
{
    char a[MAX];
    char b[MAX];
    scanf("%s",&a);   
    scanf("%s",&b);
    printf("%d\n",StringCompare(b,a));
    getchar();getchar();
    return 0;
}
总是超时
哪位高手来解答一下~!!
谢谢

TOP

0
这东西你还发这啊,超时应该是死循环了,你怎么不用ide环境的debug看看呵。每过一条变量的值都能看到,哪出问题了不就看出来了吗

TOP

当前时区 GMT+8, 现在时间是 2025-3-20 18:29