Recursion: Codevs 1251 Brackets

Source: Internet
Author: User

Codevs 1251 Bracketstime 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 /*instead of trying to find out what the pattern looks like from the output of the sample data, it is actually putting the first few combinations of these strings (because there are multiple cases for each combination, so the string array is returned), and then merging the two, then the length, bounds, and string of the combination before and after the enumeration, directly returning, Two strings, with multiplication sign in the middle, with parentheses on both sides to return*/2#include <iostream>3 using namespacestd;4#include <cstdio>5#include <vector>6vector<string> Dfs (string*SS,intBeginintend)7 {8vector<string>ret;9     if(Begin>end)returnret;Ten     if(begin==end) One     { A Ret.push_back (Ss[begin]); -         returnret; -     } the     if(begin+1==end) -     { -         strings='('+ss[begin]+'*'+ss[end]+')'; - Ret.push_back (s); +         returnret; -     } +     intsize1,size2; A      for(inti=begin;i<end;++i) at     { -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) -            for(intk=0; k<size2;++k) in           { -               strings='('+s1[j]+s2[k]+')';/*Notice that there is no multiplication sign in the middle because there is no multiplication sign between the brackets and the parentheses in the title .*/ to Ret.push_back (s); +           } -     } the     returnret; * } $ intMain ()Panax Notoginseng { -     stringss[ the]; the     intN; +scanf"%d",&n); A      for(intI=1; i<=n;++i) theCin>>Ss[i]; +vector<string>ans=dfs (SS,1, n); -     intSize=ans.size (); $      for(intI=0; i<size;++i) $cout<<ans[i]<<Endl; -     return 0; -}

Recursion: 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.