本题实质是一个单纯的DFS,而且也不用剪枝,直接暴搜 + 枚举即可。仔细分析本题,可以对递归算法的实现有更深的理解。特别能帮助理解,递归算法层层深入,然后可能回溯返回的过程。如何保存递归路径也是经常会遇到的问题。
显然,当取到最优解的时候,纸片被分成若干段。并且,这若干段的和与目标数字最接近。因此,求最优解的过程就是依次确定第一个切割位置、第二个切割位置等等。
首先决定第一次切割的位置,这时纸片被分成两个部分,计算当前两部分数字和。如果满足条件,则记录当前结果。然后,对第一次切割剩下的右半部分,再寻找第二次切割的位置。依次下去,直到分割位置到达字符串末尾。因为在每一次分割中可能有多个可选的位置,我们当前只使用了第一个,所以我们需要回溯。也就是,从下一层回到当前层,尝试当前层的下一个可能位置。回溯的另一个方面表现在分割路径的记录。
题目要求打印具体的分割情况,所以递归求解的过程需要记录每次的分割位置。每当取得一个可行解的时候,记录可行解的分割路径。当下一个可行解比当前解更优时,替换分割路径。
这里需要注意一下:有些情况,可能根本不分割就可以取得最有解,如10, 9。
#include <iostream> #include <string> #include <vector> using namespace std; //***********************常量定义***************************** const int INF = 999999999; //*********************自定义数据结构************************* //********************题目描述中的变量************************ int target; string strValue; //**********************算法中的变量************************** int ans = INF; //保存方案的当前分割点 vector<int> curPath; //保存可行方案的分割点 vector<int> path; //标记解是否唯一 bool notOnlySolve; //***********************算法实现***************************** //target为目标数,strValue为纸条上的数 //startPos为当前搜索的起始位置 //curSum为当前已分割开的数字的总和 bool Search( int target, string strValue, int startPos, int curSum ) { int iSize = (int)strValue.size(); //从起始位置开始搜索,并枚举所有分割点 for( int i=startPos; i<iSize-1; i++ ) { curPath.push_back( i ); int iLeft = atoi( ( strValue.substr( startPos, i-startPos+1 ) ).c_str() ); int iRight = atoi( ( strValue.substr( i+1, (iSize-1)-(i+1)+1 ) ).c_str() ); int sum = curSum + iLeft + iRight; //如果分割方案可行 if( sum <= target && target - sum <= ans ) { //如果与以前某个方案的结果相同 if( target - sum == ans ) notOnlySolve = true; else { notOnlySolve = false; ans = target - sum; //保存可行方案的分割点 path.clear(); path.insert( path.begin(), curPath.begin(), curPath.end() ); } } //DFS递归调用 Search( target, strValue, i+1, curSum+iLeft ); //回溯,在当前搜索深度下,枚举下一个分割方案 curPath.pop_back(); } if( ans == INF || notOnlySolve ) return false; else return true; } void PrintPath() { if( path.empty() ) return; int i, j; for( i=0; i<=path[0]; i++ ) { cout << strValue[i]; } cout << " "; int size = (int)path.size(); for( i=1; i<size; i++ ) { for( j=path[i-1]+1; j<=path[i]; j++ ) { cout << strValue[j]; } cout << " "; } for( j=path[size-1]+1; j<strValue.size(); j++ ) { cout << strValue[j]; } cout << endl; } //************************main函数**************************** int main() { freopen( "in.txt", "r", stdin ); while( cin >> target >> strValue, target != 0 && strValue != "0" ) { //如果纸条上的数和目标数相等 int iValue = atoi( strValue.c_str() ); if( target == iValue ) { cout << target << " " << target << endl; } else { //复原全局变量 notOnlySolve = false; path.clear(); curPath.clear(); ans = INF; // 如果有解且唯一 if( Search( target, strValue, 0, 0 ) ) { //如果不分割 if( ( target > iValue ) && ( target - iValue <= ans ) ) cout << iValue << " " << iValue << endl; else { cout << target - ans << " "; PrintPath(); } } else { //解不唯一 if( notOnlySolve ) cout << "rejected" << endl; //无解 else { if( ( target > iValue ) && ( target - iValue <= ans ) ) cout << iValue << " " << iValue << endl; else cout << "error" << endl; } } } } return 0; }