Codevs 1251 Brackets

Source: Internet
Author: User

time limit: 1 sspace limit: 128000 KBtitle level: Golden GoldTitle Description Description

When calculating multiplication, we can add parentheses to change the order of multiplication, such as calculating X1, X2, X3, X4, ..., xn, can

(X1 (X2 (X3 (X4 (... (XN-1*XN)))))

:::

:::

(((... (((X1*X2) X3) X4) ...) XN-1) XN)

Your task is to program all such braces.

Enter a description Input Description

The first line of the input file is a number n (1<=n<=10), which indicates that there are n variables, followed by the name of one variable for each row of n rows.

Output description Output Description

Outputs all of the scenarios with parentheses added. Note: Do not add parentheses to a single character, two characters typeface by the middle of the multiplication sign.

Sample input Sample Input

4

North

South

East

West

Sample output Sample Output

(North (East*west))

(North (South*east) West)

((North*south) (east*west))

(North (South*east)) West)

(((North*south) East) West)

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <vector>5 6 using namespacestd;7vector<string> Dfs (string*SS,intBeginintend)8 {9vector<string>ret;Ten     if(Begin>end)returnret; One     if(begin==end) A     { - Ret.push_back (Ss[begin]); -         returnret; the     } -     if(begin+1==end) -     { -         strings='('+ss[begin]+'*'+ss[end]+')';//The middle of the merge + Ret.push_back (s); -         returnret; +     } A     intsize1,size2; at      for(inti=begin;i<end;i++) -     { -vector<string> s1=DFS (ss,begin,i); -vector<string> S2=dfs (ss,i+1, end); -Size1=s1.size (); size2=s2.size (); -          for(intj=0; j<size1;j++) in         { -              for(intk=0; k<size2;k++) to             { +                 strings='('+s1[j]+s2[k]+')';//Note that this is a follow-up merger without multiplication sign. - Ret.push_back (s); the             } *         } $     }Panax Notoginseng     returnret; - } the intMain () + { A     stringss[ the]; the     intN; +scanf"%d",&n); -      for(intI=1; i<=n;i++) $Cin>>Ss[i]; $vector<string> Ans=dfs (SS,1, n); -     intSize=ans.size (); -      for(intI=0; i<size;i++) thecout<<ans[i]<<Endl; -     return 0;Wuyi}

Codevs 1251 Brackets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.