感觉像深搜……
程序代码:
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <cstdlib> using namespace std; string str; int len; int cnt; void DFS(int start ,int end,int deep,string & result) { if(deep == len - 1) { cout << result << endl; cnt++; return ; } if(start > 0) { string temp = result; result.insert(0,str.substr(start-1,1)); result.insert(0,"("); result.append(")"); DFS(start - 1,end,deep+1,result); result = temp; } if(end < len - 1) { string temp = result; result.insert(result.length(),str.substr(end+1,1)); result.insert(0,"("); result.append(")"); DFS(start,end+1,deep+1,result); result = temp; } } int main() { cin >> str; len = str.length(); for(int i = 0; i < len-1; i++){ string t = str.substr(i,2); t.insert(0,"("); t.append(")"); DFS(i,i+1,1,t); } cout << "cnt = " << cnt ; return 0; }