Expression 栈 + 搜索

Expression

Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

You shoule remove valid pairs of parentheses from a valid arithmetic expression,and you will get many expressions. For example, given (2+(2*2)+2), you can get (2+2*2+2), 2+(2*2)+2, and 2+2*2+2. (2+2*2)+2 and 2+(2*2+2) can’t be reached. More than one pairs of parentheses surround one Subexpression. 

Input

The first line contains an integer T(1 <= T <= 10), indicating the number of test cases.For each test case ,the first and only line of input contains one valid arithmetic expression (1≤length of expression ≤200)composed of nonnegative integers, four basic arithmetic operations (‘+’,‘-’,‘*’, ‘/’), and parentheses(‘(’,’)’, 1≤pairs of parentheses≤10). 

Output

For each test case, output all expressions correctly removed the brackets ,sorted lexicographically. 

Sample Input

3
(0/(0))
(2+(2*2)+2)
(1+(2*(3+4)))

Sample Output

(0/0)
0/(0)
0/0
(2+2*2+2)
2+(2*2)+2
2+2*2+2
(1+(2*3+4))
(1+2*(3+4))
(1+2*3+4)
1+(2*(3+4))
1+(2*3+4)
1+2*(3+4)
1+2*3+4

算法:
1.由栈求出括号匹配编号。
2.DFS找出所有可能。
3.set既可去重又可自动排序
View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<stack>
#include<map>
#include<string>
#include<vector>
#include<set>
using namespace std;
int N, M, len;
char str[1000];
int hash[1000];

struct node
{
  int x,y;
}st;

map<int,node>mp;
stack<int>p;
set<string>Mystring;

void print(  )
{  
  char temp[30];
  int k = 0;
  for(int i = 0; i < len; i++)
  {
     if( !hash[i] )
         temp[k++] = str[i];  
         //printf("%c",str[i]);        
          
  } 
  temp[k] = '\0';
  Mystring.insert(temp);    
  //puts("");
     
}

void pre( int p, int sum )
{
    if (p == sum) {
        print();
        return;
    }
    
    pre(p+1, sum);
    node t = mp[p+1];
    hash[t.x] = hash[t.y] = 1;
    pre(p+1, sum);
    hash[t.x] = hash[t.y] = 0;  
}

void solve( )
{
 len = strlen(str);
 int ans = 0;
 for( int i = 0; i < len; i++)
 { 
     if( str[i] == ')' )
     {
        st.y = i;
        while( !p.empty( ) )
        {
            int q = p.top( );
            p.pop( );
            if( str[q] == '(' )
            {
              st.x = q;       
              break; 
            }  
        }
        ++ans;         
        mp.insert(pair<int,node>(ans,st)); 
     }
     else if( str[i] == '(')
     p.push(i);   
 }    
/* for (int i = 1; i <= ans; ++i) {
    printf("%d %d\n", mp[i].x, mp[i].y);
 }  */
 
 pre(0, ans);    
}

int main( )
{
  scanf("%d",&N);
  while(N--)
  { 
     memset(hash,0,sizeof(hash));
     Mystring.clear();
     while( !p.empty() )
        p.pop( );
     mp.clear( );
     scanf("%s",str);
     solve( );
    // sort(Mystring.begin(), Mystring.end());
     set<string>::iterator it, its;
     for( it = Mystring.begin(), its = ++it; its != Mystring.end(); its++)
     {  
        cout<<*its<<endl;
     }
  }
  return 0;
}

 



posted on 2012-07-30 22:43  more think, more gains  阅读(189)  评论(0编辑  收藏  举报

导航