首页 > 其他分享 >HDU-1159-Common Subsequence

HDU-1159-Common Subsequence

时间:2023-02-02 12:03:21浏览次数:64  
标签:HDU 1159 int max len1 len2 Subsequence maxn 题目


​题目链接​​​
题目大意:给出两个字符串,求两个字符串的最长公共字串。
思路:慢慢重心开始有贪心转向动态规划了,这题就是简单的动态规划题。以题目的第一组测试数据为例。abcfbc
abfcab。
​​点击这里看图解更容易理解​​
从图中明显可以看出:
F[i][j]=F[i-1][j-1]+1;(a[i]==b[j])
F[i][j]=max(F[i-1][j],F[i][j-1])(a[i]!=b[j]);
n由于F(i,j)只和F(i-1,j-1), F(i-1,j)和F(i,j-1)有关, 而在计算F(i,j)时, 只要选择一个合适的顺序, 就可以保证这三项都已经计算出来了, 这样就可以计算出F(i,j). 这样一直推到f(len(a),len(b))就得到所要求的解了.

#include<stdio.h>
#include<string.h>
#define maxn 1000+10
char a[maxn],b[maxn];
int f[maxn][maxn];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
while(~scanf("%s %s",a,b))
{
int len1,len2,i,j;
len1=strlen(a);
len2=strlen(b);
memset(f,0,sizeof(f));//初始化个位置为零
for(i=1;i<=len1;i++)
for(j=1;j<=len2;j++)
{
if(a[i-1]==b[j-1])
f[i][j]=max(f[i-1][j-1],f[i-1][j-1]+1);
else
f[i][j]=f[i-1][j]>f[i][j-1]?f[i-1][j]:f[i][j-1];
}
printf("%d\n",f[len1][len2]);
}
}


标签:HDU,1159,int,max,len1,len2,Subsequence,maxn,题目
From: https://blog.51cto.com/u_14235050/6033445

相关文章

  • HDU-4552-怪盗基德的挑战书
    怪盗基德的挑战书TimeLimit:3000/1000ms(Java/Other)MemoryLimit:65535/32768K(Java/Other)TotalSubmission(s):26AcceptedSubmission(s):10Font:Time......
  • hdu-4883- (Best Coder) TIANKENG’s restaurant
    TIANKENG’srestaurantTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:131072/65536K(Java/Others)TotalSubmission(s):1622    AcceptedSubmi......
  • HDU-1686-Oulipo
    OulipoTimeLimit:3000/1000ms(Java/Other)   MemoryLimit:32768/32768K(Java/Other)TotalSubmission(s):98   AcceptedSubmission(s):63Problem......
  • HDU-1272-小希迷宫
    ​​题目链接​​​小希的迷宫TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):34355AcceptedSubmission(s......
  • HDU-1232-畅通工程(未完待续)
    畅通工程TimeLimit:4000/2000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):34508AcceptedSubmission(s):18267ProblemDescri......
  • HDU-1865-1sting
    1stingTimeLimit:5000/1000MS(Java/Others)MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):3997AcceptedSubmission(s):1489ProblemDescriptio......
  • HDU-1286-找新朋友
    ​​题目链接​​找新朋友TimeLimit:2000/1000ms(Java/Other)MemoryLimit:65536/32768K(Java/Other)TotalSubmission(s):5AcceptedSubmission(s):2Font......
  • HDU-2089-不要62
    ​​点这里查看题目​​#include<stdio.h>#definemaxn1000010intf[maxn];intweishu(inta){intb=1;while(1){if(a/10==0)break;......
  • poj-1458-Common Subsequence
    CommonSubsequenceTimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:43207Accepted:17522DescriptionAsubsequenceofagivensequenceisthegivenseq......
  • HDU-1251-统计难题(未完待续 还有两种方法还没整理)
    统计难题统计难题TimeLimit:4000/2000MS(Java/Others)MemoryLimit:131070/65535K(Java/Others)TotalSubmission(s):22667AcceptedSubmission(s):9545Proble......