题目链锁:http://acm.hdu.edu.cn/showproblem.php?pid=4105
题意: 给出 一串数字,最多能插入多少个空格使它变成电波的形式
dp[i][j][0]表示把i到j作为波谷时最多能插入多少个空格,dp[i][j][1]表示i到j作为波峰时最多能插入多少个空格
#include<iostream> #include<cstring> #include<string> #include<cstdio> #include<stdio.h> #include<algorithm> #include<cmath> #include<set> #include<map> #include<queue> #include<vector> using namespace std; #define inf 0x3f3f3f3f #define eps 1e-9 #define mod 1000000007 #define FOR(i,s,t) for(int i = s; i < t; ++i ) #define REP(i,s,t) for( int i = s; i <= t; ++i ) #define LL long long #define ULL unsigned long long #define pii pair<int,int> #define MP make_pair #define lson id << 1 , l , m #define rson id << 1 | 1 , m + 1 , r #define maxn ( 400 + 10 ) #define maxe ( 50000+10 ) int dp[111][111][2], a[111]; int cmp ( int i, int j, int l, int r ) { while( a[i] == 0 && i < j ) ++i ; while( a[l] == 0 && l < r ) ++l; if( j - i < r - l ) return 0; if( j - i > r - l ) return 1; while( i <= j ) { if( a[i] > a[l] ) return 1; if( a[i] < a[l] ) return 0; ++i, ++l; } return -1; } char s[111]; int main () { int n; while( scanf("%d", &n ) != EOF ) { scanf("%s", s ); for( int i = 0 ; i < n; i++ ) a[i] = s[i]-'0'; memset( dp, 0, sizeof( dp ) ); //for( int i = 0; i < n; i++ ) //dp[i][n-1][0] = dp[i][n-1][1] = 1; for( int i = n-2; i >= 0; i-- ) { for( int j = i; j < n-1; j++ ) { int l = j + 1; for( int r = l; r < n; r++ ) { int temp = cmp( i, j, l, r ); if( temp == 1 ) dp[i][j][1] = max( dp[i][j][1], dp[l][r][0] + 1 ); if( temp == 0 ) dp[i][j][0] = max( dp[i][j][0], dp[l][r][1] + 1 ); } } } int ans = 0; for( int i = 0 ; i < n; i++ ) ans = max( ans, dp[0][i][0] ); printf( "%d\n", ans ); } return 0; }