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. SIX GOD

    SIX GOD是什么意思呢.?_百度知道 SIX GOD

  2. [免费活动通知]RAD Studio XE8 技术研讨会(上海、成都)

     活动类型:免费研讨会 报名链接: http://forms.embarcadero.com/AP15Q3CNRADStudioDeepDiveSeminar 上海 2015 年 8 月 13 日 ...

  3. EasyUI - DataGrid 组建 - [ 删除,修改 ]

    效果: html代码: <div style="padding-top: 50px; width: 800px; margin: 0 auto;"> <!--使用 ...

  4. Eclipse用法和技巧四:生成说明文档1

    写代码经常要添加注释的,java代码的注释也可以分为两种.单行注释以“//”作为开头就可以.段落注释在第一行开头添加“/*”,在最后一行结尾添加“*/"即可.这里介绍一种添加注释的方法,其注 ...

  5. [cocos2d-x]HelloWorldDemo

    实现一个demo,具备以下功能: 1.让几个字分别位于中间和四个角落. 2.中间的字体改变,并且带有闪烁功能. 3.单点触摸和多点触摸,并且能够实现滑动效果,滑动的话必须使用带有bool返回值的单点触 ...

  6. hdu1298 T9(手机输入法,每按一个数字,找出出现频率最高的字串,字典树+DFS)

    Problem Description A while ago it was quite cumbersome to create a message for the Short Message Se ...

  7. Android Folding View(折叠视图、控件)

    版本号:1.0 日期:2014.4.21 版权:© 2014 kince 转载注明出处 非常早之前看过有人求助以下这个效果是怎样实现的,   也就是側滑菜单的一个折叠效果,事实上关于这个效果的实现,谷 ...

  8. 基于visual Studio2013解决面试题之0208二叉搜索树后序遍历序列

     题目

  9. 解决删除Volume报错的问题

    很久没有遇到过删除Volume出错使得Volume处于Error_Deleting状态的情况了,昨天删除一个Volume时又出现了这个问题,这里顺便把解决方法记录一下. 注意我这里针对的是后端采用is ...

  10. 在tornado中使用celery实现异步任务处理之中的一个

    一.简单介绍 tornado-celery是用于Tornado web框架的非堵塞 celeryclient. 通过tornado-celery能够将耗时任务增加到任务队列中处理, 在celery中创 ...