动态规划 DP

10.1.5.253    1143    数字金字塔
#include <iostream> #include<string.h> using namespace std; int a[1005][1005],b[1005][1005]; int n; int max(int x, int y) { return x>y?x:y ; } int f(int i,int j) { if(b[i][j]!=-1) return b[i][j]; return b[i][j]=a[i][j]+max(f(i+1,j),f(i+1,j+1)); } int main() { int i,j; while(cin>>n) { for(i=0;i<n;i++) for(j=0;j<=i;j++) cin>>a[i][j]; memset(b,-1,sizeof(b)); for(j=0;j<n;j++) b[i-1][j]=a[i-1][j]; cout<<f(0,0)<<endl; } }



17 nyist
//               Accept
//记忆式搜索 #include <iostream> #include<stdio.h> #include<string.h> using namespace std; char a[10002]; int b[10002]; int n,ans; int f(int x) { int i,t; if(b[x]>0) return b[x]; b[x]=1; for(i=0;i<=x-1;i++) { t=f(i); if(a[i]<a[x] && b[x]<t+1) b[x]=t+1; } return b[x]; } int main( ) { int i,j,len; cin>>n; for(i=1;i<=n;i++) { scanf("%s",a); len=strlen(a) ; for(j=0;j<len;j++) b[j]=-1; // 设置一个不可能出现的结果 f(len-1); ans=1; for(j=0;j<len;j++) if(ans<b[j]) ans=b[j]; cout<<ans<<endl ; } }

//  DP

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char a[10002];
int  b[10002];
int n,ans;
int dp(int x)
{
	int i,j,max;
	b[0]=1;
	
	for(i=1;i<=x;i++)
	{max=1;
	for(j=0;j<i;j++)
	if(a[j]<a[i] && max<b[j]+1) max=b[j]+1;
	b[i]=max;
	}
}

int main( )
{
int i,j,len;
cin>>n;
for(i=1;i<=n;i++)
{
 scanf("%s",a);
 len=strlen(a)	;
 
 dp(len-1);
 ans=1;
 for(j=0;j<len;j++)
 if(ans<b[j]) ans=b[j];
 cout<<ans<<endl ;
}	
	
}



******************************************************************88

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char a[10002];
int  b[10002];
int n,ans;
int dp(int x)
{
	int i,j,max;
	ans=b[0]=1;
	
	for(i=1;i<=x;i++)
	{max=1;
	for(j=0;j<i;j++)
	if(a[j]<a[i] && max<b[j]+1) max=b[j]+1;
	b[i]=max;
	if(b[i]>ans)  ans=b[i];
	}
}

int main( )
{
int i,j,len;
cin>>n;
for(i=1;i<=n;i++)
{
 scanf("%s",a);
 len=strlen(a)	;
 
 dp(len-1);
 
 cout<<ans<<endl ;
}	
	
}

  

poj 1088滑雪     //DP

 #include <iostream>
 using namespace std;
  
 int a[102][102],b[102][102];
 int r, c,ans;
 int f( int i, int j ) 
 {  int max=1,t;
 	if( b[i][j] > 1 )   return b[i][j];
    if( i + 1 <= r && a[i][j] > a[i+1][j] )
    {   t = f( i + 1, j ) + 1;
        if( max < t )   max = t;
    }
    if( j + 1 <= c && a[i][j] > a[i][j+1] )
    {   t = f( i, j + 1 ) + 1;
        if( max < t )    max = t;
    }
    if( i - 1 > 0 && a[i][j] > a[i-1][j] )
    {   t = f( i - 1, j ) + 1;
        if( max < t )   max = t;
    }
    if( j - 1 > 0 && a[i][j] > a[i][j-1] )
    {   t = f( i, j - 1 ) + 1;
        if( max < t)  max = t;
    } 
    return max;
}

int main()
{	int i,j;
    cin >> r >> c;
    for ( i = 1; i <= r; ++i )
      for ( j = 1; j <= c; ++j )
       { cin >> a[i][j];  b[i][j] = 1; }
    ans = 1;
    for( i = 1; i <= r; ++i )
        for( j = 1; j <= c; ++j )
        {   b[i][j] = f( i, j );
            if( ans < b[i][j] ) ans = b[i][j];
        }
    cout << ans <<endl;
}

  

//2084  hdu  c++   数塔
#include <iostream>
#include<string.h>
using namespace std; int a[105][105],b[105][105]; int n; int max(int x, int y) { return x>y?x:y ; } int f(int i,int j) { if(b[i][j]!=-1) return b[i][j]; return b[i][j]=a[i][j]+max(f(i+1,j),f(i+1,j+1)); } int main() { int i,j,c; cin>>c; while(c--) { cin>>n ; for(i=0;i<n;i++) for(j=0;j<=i;j++) cin>>a[i][j]; memset(b,-1,sizeof(b)); for(j=0;j<n;j++) b[i-1][j]=a[i-1][j]; cout<<f(0,0)<<endl; } }

  

  

  

转载于:https://www.cnblogs.com/wc1903036673/p/3427931.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值