↓ 跳过正文
  1. Posts/

poj 2955 Brackets(区间dp....括号匹配。。。人生第一道区间dp)

·683 字·2 分钟

poj2955题目链接

题意:给出若干括号,问最大匹配数是多少。

思路:没有思路。我知道这是dp。。。然后其他就什么都不知道了。。。转移方程? 完全没思路。。知道了转移方程。。。。嗯,还是不会。。。边界怎么写?状态怎么推?循环顺序? 循环次序?我一点思路都没有。。。。。

人生中第一道区间dp(这话我都说了不知道多少次了。。。每次都学不会。。。。sad)

我的dp水平和其他部分的水平还真是不匹配。。。

看了题解。。。自己写(抄)了一遍。。还是觉得好玄学。。。

细节见代码。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月25日 星期一 15时12分28秒
 4File Name :code/poj/2955.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=105;
34char st[N];
35int dp[N][N];
36
37bool check(char a,char b)
38{
39    //cout<<"a:"<<a<<" b:"<<b<<endl;
40    if((a=='['&&b==']')||(a=='('&&b==')')) return true;
41    return false;
42
43}
44int main()
45{
46	#ifndef  ONLINE_JUDGE
47	freopen("code/in.txt","r",stdin);
48  #endif
49
50	while (~scanf("%s",st))
51	{
52	    if (st[0]=='e') break;
53	    ms(dp,0);
54	    int len = strlen(st);
55
56	    for (  int l =  1 ; l < len ; l++)  //枚举区间长度,区间dp好像经常要这样?
57						//区间长度要从小到大枚举,原因是计算较长区间的答案的时候,需要用到较短区间的值。
58						//所以要先算所有区间为1的,再算所有区间为2的...
59		for ( int i = 0 ,j = l  ; j < len ; i++,j++)
60		{
61			if(check(st[i],st[j]))
62			    dp[i][j] = dp[i+1][j-1]+2;  //如果当前匹配,那么匹配数在原有的基础(区间i+1到j-1)上+2
63
64			for ( int k = i ;  k < j ; k++)  //由于区间具有累加性,意思是区间[i,j]的匹配数等于区间[i,k]和区间[k+1,j]的匹配数的和。
65			    dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]); //但是我们不知道在哪里划分区间最大,所以枚举划分的点,取最大。
66		//    cout<<"dp[i][j]:"<<dp[i][j]<<endl;
67		}
68
69	    printf("%d\n",dp[0][len-1]);
70
71
72	}
73
74  #ifndef ONLINE_JUDGE
75  fclose(stdin);
76  #endif
77    return 0;
78}

相关文章

hdu 1160 FatMouse's Speed (最长上升子序列)

·769 字·2 分钟
FatMouse’s Speed # Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10172 Accepted Submission(s): 4521 Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

hdu 1114 - Piggy-Bank (完全背包)

·774 字·2 分钟
F - Piggy-Bank **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

hdu 1087 - Super Jumping! Jumping! Jumping! (最长上升子序列)

·536 字·2 分钟
E - Super Jumping! Jumping! Jumping! **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path. Your task is to output the maximum value according to the given chessmen list.

BZOJ 1652: [Usaco2006 Feb]Treats for the Cows (区间dp)

·1352 字·3 分钟
1652: [Usaco2006 Feb]Treats for the Cows # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 290 Solved: 226 [Submit][Status][Discuss] Description # FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many reasons: * The treats are numbered 1..N and stored sequentially in single file in a long box that is open at both ends. On any day, FJ can retrieve one treat from either end of his stash of treats. * Like fine wines and delicious cheeses, the treats improve with age and command greater prices. * The treats are not uniform: some are better and have higher intrinsic value. Treat i has value v(i) (1 <= v(i) <= 1000). * Cows pay more for treats that have aged longer: a cow will pay v(i)*a for a treat of age a. Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the greatest value FJ can receive for them if he orders their sale optimally? The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.

BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster (dp,类似01背包)

·1109 字·3 分钟
1649: [Usaco2006 Dec]Cow Roller Coaster # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 504 Solved: 265 [Submit][Status][Discuss] Description # The cows are building a roller coaster! They want your help to design as fun a roller coaster as possible, while keeping to the budget. The roller coaster will be built on a long linear stretch of land of length L (1 <= L <= 1,000). The roller coaster comprises a collection of some of the N (1 <= N <= 10,000) different interchangable components. Each component i has a fixed length Wi (1 <= Wi <= L). Due to varying terrain, each component i can be only built starting at location Xi (0 <= Xi <= L-Wi). The cows want to string together various roller coaster components starting at 0 and ending at L so that the end of each component (except the last) is the start of the next component. Each component i has a “fun rating” Fi (1 <= Fi <= 1,000,000) and a cost Ci (1 <= Ci <= 1000). The total fun of the roller coster is the sum of the fun from each component used; the total cost is likewise the sum of the costs of each component used. The cows’ total budget is B (1 <= B <= 1000). Help the cows determine the most fun roller coaster that they can build with their budget.