(North(South(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 namespace std;
7 vector<string> dfs(string *ss,int begin,int end)
8 {
9 vector<string> ret;
10 if(begin>end) return ret;
11 if(begin==end)
12 {
13 ret.push_back(ss[begin]);
14 return ret;
15 }
16 if(begin+1==end)
17 {
18 string s=‘(‘+ss[begin]+‘*‘+ss[end]+‘)‘;//最中间的合并
19 ret.push_back(s);
20 return ret;
21 }
22 int size1,size2;
23 for(int i=begin;i<end;i++)
24 {
25 vector<string> s1=dfs(ss,begin,i);
26 vector<string> s2=dfs(ss,i+1,end);
27 size1=s1.size();size2=s2.size();
28 for(int j=0;j<size1;j++)
29 {
30 for(int k=0;k<size2;k++)
31 {
32 string s=‘(‘+s1[j]+s2[k]+‘)‘;// 注意题目中说这里是没有乘号的 后续的合并
33 ret.push_back(s);
34 }
35 }
36 }
37 return ret;
38 }
39 int main()
40 {
41 string ss[15];
42 int n;
43 scanf("%d",&n);
44 for(int i=1;i<=n;i++)
45 cin>>ss[i];
46 vector<string> ans=dfs(ss,1,n);
47 int size=ans.size();
48 for(int i=0;i<size;i++)
49 cout<<ans[i]<<endl;
50 return 0;
51 }