码迷,mamicode.com
首页 > 其他好文 > 详细

topcoder SRM628 div2 500(转)

时间:2014-07-29 12:41:06      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   strong   io   for   cti   

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(转),布布扣,bubuko.com

topcoder SRM628 div2 500(转)

标签:style   blog   color   os   strong   io   for   cti   

原文地址:http://www.cnblogs.com/qiucz/p/3874899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!