Expression 暴力枚举

算法:

可以枚举所有状态,然后排序。

View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<stack>
#include<string>
using namespace std;

char str[1000];
int f[1000];

int main( )
{
   int T;
   scanf("%d",&T);
   while( T-- )
   {
     scanf("%s",str);
     int len = strlen(str);
     memset(f,0,sizeof(f));
     int ans = 0;
     stack<int>p;
     for( int i = 0; i < len; i++)
     {
        if( str[i] == '(' )
        {
           p.push(ans);
           f[i] = ans;
           ans++;    
        }
        else if( str[i] == ')' )
        {
            f[i] =  p.top();
            p.pop( );
        }
     }
     vector<string>my;
     for( int i = 1; i < (1<<ans); i++)
     {
       string tt ="";
       for( int j = 0; j < len; j++)
       {
          if( (str[j] == ')' || str[j] == '(') && (i & (1<<f[j])) )
              continue;
          tt += str[j];         
       }     
       my.push_back( tt );     
     }
     sort( my.begin(), my.end());
     for( int i = 0; i < (int) my.size(); i++)
     {
       if(i == 0 || my[i] != my[i-1] )
           cout<<my[i]<<endl;    
          
     }
     
   }
   return 0; 
}

posted on 2012-07-31 07:50  more think, more gains  阅读(195)  评论(0编辑  收藏  举报

导航