我知道这个解决方案不合理,但它是有效的。此处提供此方法是为了作为当前问题解决方案的一种变体。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int maximumSize=40;
vector<int> visited(maximumSize, 0);
string word;
void showContentVectorString(vector<string>& input)
{
for(int i=0; i<input.size(); ++i)
{
cout<<input[i]<<", ";
}
return;
}
void dfs(int current, int previous, string& input, vector<string>& output, char symbol)
{
if(visited[current]==1)
{
return;
}
visited[current]=1;
string stringSymbol;
stringSymbol.push_back(symbol);
if(input[current]!=stringSymbol[0])
{
word.push_back(input[current]);
}
else
{
output.push_back(word);
word.clear();
}
if(current==(input.size()-1))
{
output.push_back(word);
word.clear();
}
for(int next=(current+1); next<input.size(); ++next)
{
if(next==previous)
{
continue;
}
dfs(next, current, input, output, symbol);
}
return;
}
void solve()
{
string testString="this_is_a_test_string";
vector<string> vectorOfStrings;
dfs(0, -1, testString, vectorOfStrings, '_');
cout<<"vectorOfStrings <- ";
showContentVectorString(vectorOfStrings);
return;
}
int main()
{
solve();
return 0;
}
结果如下:
vectorOfStrings <- this, is, a, test, string,