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

 

hdu4105  Electric wave的更多相关文章

  1. 简单dp hdu-4105-Electric wave

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4105 题目意思: 给一个字符串,求最大的分隔空格数,记所有被分隔的数为a1,a2,a3,..... ...

  2. RIFF和WAVE音频文件格式

    RIFF file format RIFF全称为资源互换文件格式(Resources Interchange File Format),是Windows下大部分多媒体文件遵循的一种文件结构.RIFF文 ...

  3. IEEE 802.11p (WAVE,Wireless Access in the Vehicular Environment)

    IEEE 802.11p(又称WAVE,Wireless Access in the Vehicular Environment)是一个由IEEE 802.11标准扩充的通讯协定.这个通讯协定主要用在 ...

  4. Wave - 花たん 音乐

    Wave 歌手:花たん 所属专辑:Flower 間違えて宇宙終わって(宇宙因为一个错误而终结了) 青信号はいつも通り(通行的灯号一如往常的) 飛んでまた止まって(又再停止传播) また 飛びそうだ(然后 ...

  5. Web 播放声音(AMR 、WAVE)

    最近甚是苦闷,属于边学边做,跳进了很多坑,别提有多惨了,不过结果还是不错滴,纵观前后,一句话足以概括 “痛并快乐着” ~~~ ok,我少说废话,下面来总结下 Web 播放声音一些注意事项. 说到 We ...

  6. 多媒体(2):WAVE文件格式分析

    目录 多媒体(1):MCI接口编程 多媒体(2):WAVE文件格式分析 多媒体(3):基于WindowsAPI的视频捕捉卡操作 多媒体(4):JPEG图像压缩编码 多媒体(2):WAVE文件格式分析

  7. UVa 488 - Triangle Wave

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  8. modelsim 中 WAVE窗口中能不能只显示变量名,而不显示路径

    可以的,在wave窗口左下角有一个黑色的logo,你点击它就可以省电路径,只显示port名称,再点击就切换回来了,如图红色圈圈标记的logo,你可以试试!

  9. C# 获取wave文件信息【转】

    public class WaveHelper { /// <summary> /// 数据流 /// </summary> private Stream m_WaveData ...

随机推荐

  1. Qt之开机自启动及拥有管理员权限

    源地址:http://blog.sina.cn/dpool/blog/s/blog_a6fb6cc90101feia.html Windows开机自启动的程序很多,包括系统软件.杀毒软件.一些其他安装 ...

  2. biz处理dao事务处理层

    前言 正文 1.创建一个事物管理对象,该对象将连接对象绑定到当前线程 2.dao层的代码演示样例 3.biz层处理数据库的事务 总结

  3. PHP开发-上传文件

    <?php /****************************************************************************** 参数说明: $max_ ...

  4. Python 学习入门(21)—— 线程

    本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础以及Python两个线程标准库的完整介绍及使用示例. 1. 线程基础 1.1. 线程状态 线程有5种状态,状态转换的过程如下图 ...

  5. Codeforces Round #216 (Div. 2) D. Valera and Fools

    题目链接:http://codeforces.com/contest/369/problem/D 注意题意:所有fools都向编号最小的fool开枪:但每个fool都不会笨到想自己开枪,所以编号最小的 ...

  6. 页面导出生成pdf,使用wkhtmltopdf第三方工具

    把页面导出生成pdf,这里用到第三方的工具,使用方法中文文档没有找到,网上也没找到网友详细的神作.没有深入研究,所以也不赘述了,当然最基本的使用大多数也够用了,详细参数的官网也没介绍,大家使用的时候, ...

  7. c# 文件/文件夹操作

    1.判断文件夹是否存在并创建 if (!Directory.Exists(tempFolderName)) { Directory.CreateDirectory(tempFolderName); }

  8. spring mvc3中的addFlashAttribute方法

    spring mvc3中的addFlashAttribute方法  

  9. 14.1.3 Turning Off InnoDB 关掉InnoDB

    14.1.3 Turning Off InnoDB 关掉InnoDB: Oracle 推荐InnoDB 作为首选的存储引擎用于典型的数据库应用,从单用户的wikis到博客, 到高端应用把性能推到极限. ...

  10. Yarn的ApplicationMaster管理

    首先client向ResourceManager提交程序(包括ApplicationMaster程序,ApplicationMaster启动命令,用户程序)后,ResourceManager向资源调度 ...