c语言寻找最长字串DP

来源:百度知道 编辑:UC知道 时间:2024/05/15 04:25:43
Description

所谓最长公共子串,比如串A "acedfe", 串B "aede", 则它们的最长公共子串为串 "aede", 即不论串中字符是否相邻,只要它是给定各个串都包含的,且长度最长的串。给定两个不包含空格的字符串和一整数n , 如果最长公共串长度大于或等于n,输出Not Smaller, 否则输出Smaller.

Input

第一行仅一个整数N,(1<= N <= 100).表示包含N个测试样例.
每个测试样例由两个字符串A,B( 0< strlen(A),strlen(B) <= 1000) 和一个整数n( 0 <= n <= 1000)构成.

Output

对于每个测试样例,输出Not Smaller 或 Smaller.

Sample Input

3
acedsd
acdd
2
eedfd
zzxxxx
3
feefs
as
1

Sample Output

Not Smaller
Smaller
Not Smaller

//我的代码,出现RE,数组看样子是足够大了,是不是哪些变量我没有申请内存空间?
//动态规划做的,麻烦帮看下
#include<iostream>
using namespace std;
int len(char *a,char *b)
{
int la,lb;
int i,j;
int c[1001][1001];
la=strlen(a);
lb=strlen(b);
for(i=0;i<la;i++)
for(j=0;j<lb;j++)
{
if(i==0||j==0)

#include<iostream>
using namespace std;

int len(char *a,char *b)
{
int la,lb;
int i,j;
int c[1002][1002];
la=strlen(a);
lb=strlen(b);
for(i=0;i<=la;i++) c[i][0]=0;
for(j=0;j<=lb;j++) c[0][j]=0;
for(i=1;i<=la;i++) //注意你开始的算法,当a[0]=b[0]时的c[0][0]=c[-1][-1]+1
{
for(j=1;j<=lb;j++)
{
if(*(a+i-1)==*(b+i-1))
c[i][j]=c[i-1][j-1]+1;
else
c[i][j]=c[i-1][j]>c[i][j-1]?c[i-1][j]:c[i][j-1];
}
}

return c[la][lb];
}
int main()
{
int n,l,T;
char s1[1001];
char s2[1001];
cin>>T;
if(T<1||T>100)
return 0;
while(T--)
{
cin>>s1;
cin>>s2;
cin>>n;
l=len(s1,s2);
if(l>=n)
cout<<"Not Smaller"<<endl;
else
cout<<"Smaller"<<endl;
}