codevs1251 括号

来源:互联网 发布:cydia点击软件源就闪退 编辑:程序博客网 时间:2024/05/22 03:10

1251 括号

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 

题目描述 Description

计算乘法时,我们可以添加括号,来改变相乘的顺序,比如计算X1, X2, X3, X4, …, XN的积,可以

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

:::

:::

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

你的任务是编程求出所有这样的添括号的方案。

输入描述 Input Description

输入文件第一行是一个数n(1<=n<=10),表示有n个变量,之后N行每行一个变量的名字。

输出描述 Output Description

输出所有的添加括号的方案。注意:单个字符不要加括号,两个字符相乘中间要有乘号。

样例输入 Sample Input

4

North 

South 

East 

West

样例输出 Sample Output

(North(South(East*West)))

(North((South*East)West))

((North*South)(East*West))

((North(South*East))West)

(((North*South)East)West)

思路

深搜

首先设一个动态数组vector<string>ans[11][11]。a[i][j][k]表示首单词为i,末单词为j的单词串的总种类集合。

递归的思想。从大范围往小范围紧缩,也就是从整体(1到n的单词串总种类集合)二分,先找到左侧的种类再找右侧种类,最后进行拼接。

代码

#include<iostream>#include<cstring>#include<vector>using namespace std;vector<string>ans[11][11];int n;string s[12];void dfs(int l,int r){    if(ans[l][r].size())return;    if(l==r)        ans[l][l].push_back(s[l]);    else{        for(int i=l;i<=r;i++)        {            dfs(l,i);            dfs(i+1,r);            int n1=ans[l][i].size(),n2=ans[i+1][r].size();            for(int j=0;j<n1;j++)            {                for(int k=0;k<n2;k++)                {                    string s;                    s="("+ans[l][i][j];                    if(r-l==1)                    s+="*";                    s+=(ans[i+1][r][k]+")");                    ans[l][r].push_back(s);                }            }        }    }}int main(){    cin>>n;    for(int i=1;i<=n;i++)cin>>s[i];    dfs(1,n);    int m=ans[1][n].size();    for(int i=0;i<m;i++)        cout<<ans[1][n][i]<<endl;}

 

 

 

0 0
原创粉丝点击