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#include #include 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 len2) { return 1; } else { for(c=0;c<=len1;c++) { if(str[i+c]!=str[a+c]) { if(str[i+c] 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 maxx) { maxx=dp[1][0][i]; } } printf("%d\n",maxx); } return 0;}