topcoder SRM628 div2 500(转)

Problem Statement

    
We have three types of brackets: "()", "[]", and "{}". We are now interested in some special strings. A string is special if all the following conditions hold:

  • Each character of the string is one of the six bracket characters mentioned above.
  • The characters of the string can be divided into disjoint pairs such that in each pair we have an opening bracket and a closing bracket of the same type.
  • For each pair, the opening bracket must occur to the left of the corresponding closing bracket.
  • For each pair, the substring strictly between the opening and the closing bracket must be a special string (again, according to this definition).

For example, the empty string is a special string: there are 0 pairs of brackets. The string "[]" is also a special string: there is one pair of matching brackets, they are in the proper order, and the string between them (which is the empty string) is a special string.

The character ‘X‘ (uppercase x) occurs in expression at most five times; all other characters in expression are brackets of the types mentioned above. We want to change expression into a special string by changing each ‘X‘ into one of the brackets. (Different occurrences of ‘X‘ may be changed into different brackets.) Return "possible" (quotes for clarity) if we can do that, and "impossible" otherwise.

Definition

    
Class: BracketExpressions
Method: ifPossible
Parameters: string
Returns: string
Method signature: string ifPossible(string expression)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256

Constraints

- expression will have between 1 and 50 characters, inclusive.
- Each character in expression will be ‘(‘, ‘)‘, ‘[‘, ‘]‘, ‘{‘, ‘}‘ or ‘X‘.
- There will be at most 5 occurences of ‘X‘ in expression.

Examples

0)  
    
"([]{})"
Returns: "possible"
This is already a special string. As there are no ‘X‘s, we do not get to change anything.
1)  
    
"(())[]"
Returns: "possible"
 
2)  
    
"({])"
Returns: "impossible"
 
3)  
    
"[]X"
Returns: "impossible"
Regardless of bracket type you put instead of ‘X‘, you cannot create a special string.
4)  
    
"([]X()[()]XX}[])X{{}}]"
Returns: "possible"
You can replace ‘X‘s respectively with ‘{‘, ‘(‘, ‘)‘ and ‘[‘.

题意:有三种括号 和 x,x能变成任意的括号,求能否通过变化x使得给的字符串符合括号匹配

一道中等DP题,先对每一种可能的匹配情况进行遍历,再对其松弛更新。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <vector>
#define LL long long
using namespace std;

class BracketExpressions
{
public:
   int _max(int c, int d)
   {
       return c > d?c:d;
   }
   int match(char a, char b)
   {
       if(a==‘(‘ && b==‘)‘)
       return 1;
       if(a==‘{‘ && b==‘}‘)
       return 1;
       if(a==‘[‘ && b==‘]‘)
       return 1;
       if(a==‘X‘ &&(b==‘]‘||b==‘}‘||b==‘)‘))
       return 1;
       if(b==‘X‘ && (a==‘[‘||a==‘{‘||a==‘(‘))
       return 1;
       if(a==‘X‘ && b==‘X‘)
       return 1;
       return 0;
   }
   string ifPossible(string expression)
   {
       int i, j, k, g, len;
       int d[100][100];
       string s = expression;
       len = s.size();
       memset(d, 0, sizeof(d));
       for(i = 0; i < len-1; i++)
       if(match(s[i], s[i+1]))
       d[i][i+1] = 1;
       for(k = 2; k < len; k++)
       {
           for(i = 0; i < len-k; i++)
           {
               j = i+k;
               if(match(s[i], s[j])) d[i][j] = d[i+1][j-1] + 1;
               for(g = 0; g < k; g++)
               d[i][j] = _max(d[i][i+g]+d[i+g+1][j], d[i][j]);
           }
       }
       if(2*d[0][len-1]!=len)
       return "impossible";
       else
       return "possible";
   }
};

topcoder SRM628 div2 500(转)

时间: 2024-10-13 04:19:24

topcoder SRM628 div2 500(转)的相关文章

Topcoder SRM 619 DIv2 500 --又是耻辱的一题

这题明明是一个简单的类似约瑟夫环的问题,但是由于细节问题迟迟不能得到正确结果,结果比赛完几分钟才改对..耻辱. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define ll long long using namespace std; #define NN 370000 class Choo

Topcoder SRM 630 (500 floyed 暴力 _builtin_popcount())

题意:给n个点,保证图联通,给点相连的距离,求一个最多的点,这些点之间的距离都是相同的. 分析: 下面的代码是我们房间第一的大神的,写的很简洁,我的思路和他的一样,但是我不知道错哪了. 思路是暴力枚举,最多有10个点,先用floyed计算出每两点之间的距离,然后用位运算暴力枚举, 枚举每个点是否加入进去,并用set把每个加入进去的点之间的距离加进去,如果距离唯一,说明点之间所有的距离相同, 然后用_builtin_popcount()计算二进制中多少个1 ,即表示加入了多少个点,求最大. Top

求拓扑排序的数量,例题 topcoder srm 654 div2 500

周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are numbered from 0 to N-1. Some pairs of rooms are connected by bidirectional passages. The passages have the topology of a tree. That is, there are exactly N-

Topcoder SRM632 DIV2 解题报告

250:乱搞 解题代码: 1 // BEGIN CUT HERE 2 /* 3 4 */ 5 // END CUT HERE 6 #line 7 "RunningAroundPark.cpp" 7 #include <cstdlib> 8 #include <cctype> 9 #include <cstring> 10 #include <cstdio> 11 #include <cmath> 12 #include <

[Topcoder]SRM632 div2 题解

TC第一次解出三题--当了次room leader-- 感觉这次的题比较弱,代码量也很小,都是在拼手速了 250 RunningAroundPark 题意很好懂,一圈跑道上有N棵树,现给你遇到这些树的顺序,问最少需要多少走圈才能遇到相应的序列 直接判断a[i]<=a[i+1]即可 首先假定走了一圈 #include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #inc

Topcoder SRM631 DIV2 解题报告

250:网格有两种颜色,网格中一列最长的连续的颜色相同的最大值. 解题思路:暴力. 解题代码: // BEGIN CUT HERE /* */ // END CUT HERE #line 7 "TaroGrid.cpp" #include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <

Topcoder SRM633 DIV2 解题报告

250:乱搞. 1 // BEGIN CUT HERE 2 /* 3 4 */ 5 // END CUT HERE 6 #line 7 "Target.cpp" 7 #include <cstdlib> 8 #include <cctype> 9 #include <cstring> 10 #include <cstdio> 11 #include <cmath> 12 #include <algorithm> 1

ACM学习历程—TopCoder SRM691 Div2

这是我的第一次打TC,感觉打的一般般吧.不过TC的题目确实挺有意思的. 由于是用客户端打的,所以就不发题目地址了. 300分的题: 这题大意是有一段序列只包含+和数字0~9. 一段序列的操作是,从头扫到尾,遇到+就对计数器+1.遇到数字就计算abs(num-count)的值,并加到sum中. 题目要求重新排序序列,使得sum最小. 由于是abs,最小值自然是0,于是就是要构造0. 由于计数器只会网上增,所以数字我肯定从小到大排,于是对于某个数,计数器不足这个数,那么就需要增加计数器,否则就计算a

TopCoder SRM502 Div1 500 贪心 01背包

原文链接https://www.cnblogs.com/zhouzhendong/p/SRM502-500.html SRM502 Div1 500 好题. 首先,如果已经确定了解决所有问题的优先级,只需要对于每一个问题是否被解决做出决策,那么显然直接 01 背包就好了. 事实上,我们是可以贪心地确定两个问题的优先程度的. 对于两个问题,假设分别为 a 和 b,则先做 a 再紧接着做 b 和先做 b 再紧接着做 a 的收益之差为 \[ \begin{eqnarray*} &&(-dec_a