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