zoukankan      html  css  js  c++  java
  • hdu4105  Electric wave

    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
    ",maxx);
        }
    	return 0;
    }
    

     
  • 相关阅读:
    jQuery相关知识点2
    自适应相关知识点1
    jQuery相关知识点1
    执行程序(例如UltraEdit)在WIN7下添加到右键菜单
    std::string 字符串大小写转换(转)
    Mathematica作图
    编写高效代码(大话处理器)
    关于make: *** No rule to make target `clean'. Stop.这个莫名其妙问题的解决方法
    网络编程 tcp udp 时间同步机制 代码
    python_控制台输出带颜色的文字方法
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3243870.html
Copyright © 2011-2022 走看看