↓ 跳过正文
  1. Posts/

poj 1141 Brackets Sequence (区间dp,括号匹配,记录路径)

·835 字·2 分钟

poj 1141题目链接

题意:给出一个括号序列,要求添加最少的括号,使得这个序列变成合法的括号匹配,输出最后的序列。

思路:区间dp。。。有了那么一点思路。。。我们可以用dp[i][j]表示区间[i,j]的序列最少需要添加几个符号使得匹配。。转移的话。。。和之前差不多。。dp[i][j] = dp[i+1][j-1] (s[i]与s[j])匹配。。。不匹配的话也是找中间某个点。。。初始化的话。。要变成最大值。。。比较没思路的是输出括号序列这部分。。。

参考了这篇题解:参考题解

记录路径的思路是。。。记录转移的点。。。

cut[i][j]表示的是区间[i,j]的最优值是由点cut[i][j]这里划分得到的。。。

cut[i][j]为-1表示区间[i,j]的最优值不是从中间分成两部分得到。。。

打印路径的时候。。。如果[i,j]的长度小于等于0.。直接return.

如果长度为1.。。直接输出。。。

如果长度大于1.。。。要分这段区间是否中间有划分两种情况。。具体见代码。。。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年07月25日 星期一 15时55分47秒
  4File Name :code/poj/1141.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;
 33char s[105];
 34int dp[105][105]; //dp[i][j]表示区间[i,j]最少需要添加多少字符达到匹配。
 35int cut[105][105] ; //记录一段区间是在哪里断开最优,是为了记录路径,打印括号
 36bool check(char a,char b)
 37{
 38    if (a=='['&&b==']') return true;
 39    if (a=='('&&b==')') return true;
 40    return false;
 41}
 42
 43void print(int i ,int j)
 44{
 45    if (i>j) return ;
 46    if (i==j)
 47    {
 48	if (s[i]=='('||s[i]==')') printf("()");
 49	if (s[i]=='['||s[i]==']') printf("[]");
 50	return ;
 51    }
 52    if (cut[i][j]==-1)
 53    {
 54	printf("%c",s[i]);
 55	print(i+1,j-1);
 56	printf("%c",s[j]);
 57    }
 58    else
 59    {
 60	print(i,cut[i][j]);
 61	print(cut[i][j]+1,j);
 62    }
 63}
 64int main()
 65{
 66	#ifndef  ONLINE_JUDGE
 67	freopen("code/in.txt","r",stdin);
 68  #endif
 69
 70	scanf("%s",s);
 71
 72	    int len = strlen(s);
 73	    ms(dp,0x3f);
 74	    ms(cut,-1);
 75	    for ( int i = 0 ; i <=len ; i++) dp[i][i] = 1;
 76
 77	    for ( int l = 1 ; l  < len ; l++)
 78	    {
 79		for ( int i = 0 , j = l ; j < len ; i++,j++)
 80		{
 81		    dp[i][j] = inf;
 82		    if (check(s[i],s[j]))
 83		    {
 84			dp[i][j] = dp[i+1][j-1]; //当前匹配的话,就不需要增加字符
 85			cut[i][j] = -1;
 86		    }
 87
 88		    for ( int k = i ; k < j ; k++)
 89			if (dp[i][j]>dp[i][k]+dp[k+1][j])
 90			{
 91			    dp[i][j] = dp[i][k] + dp[k+1][j];
 92			    cut[i][j] = k;
 93			}
 94		}
 95	    }
 96
 97	    print(0,len-1);
 98	    printf("\n");
 99
100
101  #ifndef ONLINE_JUDGE
102  fclose(stdin);
103  #endif
104    return 0;
105}

相关文章

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

·683 字·2 分钟
poj2955题目链接 题意:给出若干括号,问最大匹配数是多少。 思路:没有思路。我知道这是dp。。。然后其他就什么都不知道了。。。转移方程? 完全没思路。。知道了转移方程。。。。嗯,还是不会。。。边界怎么写?状态怎么推?循环顺序? 循环次序?我一点思路都没有。。。。。

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.

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.