hdu4105  Electric wave

本文介绍了一个Electricwave峰值分析的问题,旨在通过插入空白字符的方式将一串数字数据转换为合法的峰值和谷值序列,以便进一步研究。文章提供了一种动态规划解决方案,并详细解释了其实现细节。

Electric wave

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 596 Accepted Submission(s): 173

Problem Description
Ali was doing a physic experiment which requires him to observe an electric wave. He needs the height of each peak value and valley value for further study (a peak value means the value is strictly larger than its neighbors and a valley value means the value is strictly smaller than its neighbors). He did write these numbers down but he was too careless that he wrote them in a line without separations, such as “712495” may represent “7 12 4 9 5”. The only information he can remember was:
1. The data begins with a valley value
2. Each value is either a peak value or a valley value
Now he wants to insert blanks to make the data valid. If multiple solutions exist, he will choose the one with more blanks.
 

 

Input
The input consists several testcases.
The first line contains one integer N (1 <= N <= 100), the length of the data.
The second line contains one string S, the data he recorded.
S contains only digits.
 

 

Output
Print one integer, the maximum number of blanks he can insert.
 

 

Sample Input
6 712495
 

 

Sample Output
4
Hint
The separated data may have leading zeros.
 

 

Source
就是把一个字符串分成一个个小串,保证是一大一小,我们用dp[flag][i][j]表示第一位是谷还是峰,第一位是从i到j这一段,dp[flag][i][j]=fmax(dp[flag^1][j+1][k]),这样复杂度为n^3,不过,也没有什么好的优化方法,反正a这一题没有问题吧!
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
char str[105];
int dp[2][105][105];
int  compare(int i,int j,int a,int b )//前面小返回0大返回1
{
    int c;
    while(str[i]=='0'&&i<j)
    {
        i++;
    }
    while(str[a]=='0'&&a<b)
    {
        a++;
    }
    int len1=j-i,len2=b-a;
    if(len1<len2)
    {
        return 0;
    }
    else if(len1>len2)
    {
        return 1;
    }
    else
    {
        for(c=0;c<=len1;c++)
        {
            if(str[i+c]!=str[a+c])
            {
                if(str[i+c]<str[a+c])
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
        }
        return -1;
    }
}
int fmax(int a,int b)
{
    if(a>b)
    return a;
    return b;
}
int main ()
{
    int n,flag,i,j,k;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",str);
        memset(dp,0,sizeof(dp));
        {
            for(i=n-1;i>=0;i--)
            {
                for(j=i;j<n;j++)
                {
                    for(flag=0;flag<2;flag++)
                        for(k=j+1;k<n;k++)
                        {
                        if((flag^1)==compare(i,j,j+1,k))
                            dp[flag][i][j]=fmax(dp[flag][i][j],1+dp[flag^1][j+1][k]);
                        }

                }
            }
        }

        int maxx=dp[1][0][0];
        for(i=0;i<n;i++)
        {
           if(dp[1][0][i]>maxx)
           {
               maxx=dp[1][0][i];
           }
        }
        printf("%d\n",maxx);
    }
	return 0;
}

 

 

转载于:https://www.cnblogs.com/jiangu66/p/3243870.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值