• 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 ;
    }

  • 相关阅读:
    2019-6-23-win10-uwp-未给任务-GenerateAppxPackageRecipe-的必需参数-AppxManifestXml-赋值
    2018-8-17-C#-从零开始写-SharpDx-应用-控制台创建-Sharpdx-窗口
    QToolBox
    QListWidget
    宽字节 多字节 mbstowcs wcstombs
    va_start可变参数函数
    c语言二进制、八进制、十六进制
    文件锁 flock/fcntl
    volatile和锁
    串口应用程序
  • 原文地址:https://www.cnblogs.com/2014acm/p/3907992.html
Copyright © 2020-2023  润新知