zoukankan      html  css  js  c++  java
  • 1458 poj--zoj 1733---------------记忆式搜索

    #include <iostream>
    #include <string.h>
    #define N 1005
    using namespace std ;
    char s1[N],s2[N];
    int dp[N][N],ans,len1,len2;


    int max(int a,int b)
    { return a>b ? a : b ; }


    int f(int x,int y)
    {

    if (dp[x][y]!=-1)               return dp[x][y];
    if ( x==0 || y==0 )           return dp[x][y]=0;
    else     if (s1[x-1]!=s2[y-1])   return dp[x][y]=max( f(x-1,y),f(x,y-1));
                  else                        return dp[x][y]=f(x-1,y-1)+1;
    }
    int main()
    { int n,i,j ;
    while ( cin>>s1>>s2 )
    { len1=strlen(s1); len2=strlen(s2);
    memset(dp,-1,sizeof(dp));
    f(len1,len2);
    ans=0;
    for (i=0; i<=len1; i++)
    for (j=0; j<=len2; j++)
    if ( ans<dp[i][j] ) ans=dp[i][j] ;
    cout << ans << endl ;
    }
    return 0 ;
    }

    *******************************************************************************

    #include <iostream>
    #include <string.h>
    #define N 1005
    using namespace std ;
    char s1[N],s2[N];
    int a[N][N],ans,len1,len2;
    int max(int a,int b)
    { return a>b ? a : b ; }


    int f(int x,int y)
    {

    if (a[x][y]!=-1)                       return a[x][y];
    if ( x==0 || y==0 )                return a[x][y]=0;
            else      if (s1[x-1]!=s2[y-1])              return a[x][y]=max( f(x-1,y),f(x,y-1));
                    else                                         return a[x][y]=f(x-1,y-1)+1;
    }
    int main()
    {

    int n,i,j ;
    while ( cin>>s1>>s2 )
    {

    len1=strlen(s1); len2=strlen(s2);
    memset(a,-1,sizeof(a));
    f(len1,len2);
    ans=0;
    for (i=0; i<=len1; i++)
    for (j=0; j<=len2; j++)
    if ( ans<a[i][j] ) ans=a[i][j] ;
    cout << ans << endl ;
    }
    return 0 ;
    }

  • 相关阅读:
    python json模块出现Invalid control character这个异常的原因
    KMS服务,使用命令激活windows/office
    vscode Python文件头部信息
    MIMIC-III Clinical Database 翻译
    autohotkey 设置
    DeepLearning 写代码常用
    VScode 个人设置
    随机种子设置
    samba配置
    jieba 分词不显示日志
  • 原文地址:https://www.cnblogs.com/2014acm/p/3907992.html
Copyright © 2011-2022 走看看