在哪个网站做ppt模板赚钱,网站建设需要的流程,网站做树状结构有什么作用,wordpress修改上传路径1251 括号 时间限制: 1 s空间限制: 128000 KB题目等级 : 黄金 Gold题目描述 Description计算乘法时#xff0c;我们可以添加括号#xff0c;来改变相乘的顺序#xff0c;比如计算 X1, X2, X3, X4, …, XN的积#xff0c;可以 (X1(X2(X3(X4(...(XN-1… 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 输入文件第一行是一个数n1n10表示有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) 数据范围及提示 Data Size Hint 分类标签 Tags 点此展开 So,具体思路见题解好了不多说了上题解 #includeiostream
#includestring
#includevector
using namespace std;
vectorstringans[12][12];
string str[11];
int n;
void dfs(int l,int r) // l,r单词的分割数目,初始还没求得所要的串结果为空
{if(ans[l][r].size()) // 存放第一个 首单词 位置为 l 尾单词位置为r的 单词串return;if(lr) // 仅有单个单词,放到对应的位置上ans[l][l].push_back(str[l]);else{for(int il;ir;i){dfs(l,i);dfs(i1,r); // 递归求解各左右子串的划分形式。int slans[l][i].size(),srans[i1][r].size();for (int j0;jsl;j){ // 进行连接运算for (int k0; ksr;k){string s;s (ans[l][i][j];if (r-l1) //s*; // 做连接运算时加*号s(ans[i1][r][k]));ans[l][r].push_back(s); // 存入一种结果}}}}
}
int main()
{cinn;for (int i1;in;i)cinstr[i];dfs(1,n);int mans[1][n].size();for (int i0;im;i)coutans[1][n][i]endl; // 输出所有可能结果return 0;
} 如果对你有所帮助别忘了加好评哦么么哒下次见88 转载于:https://www.cnblogs.com/cangT-Tlan/p/6159034.html