C++ 输出错误:如何将字符串分解成字典中的单词

C++ 输出错误:如何将字符串分解成字典中的单词,c++,string,string-matching,C++,String,String Matching,我正在尝试实现将给定字符串分解为其组成字典单词问题的解决方案,但我的代码为字符串(如“icecreamicecream”)提供了错误的输出,其中一些单词在输出中出现了两次。请告诉我哪里出了问题。以下是我的代码: #include <set> #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include<string.h&

我正在尝试实现将给定字符串分解为其组成字典单词问题的解决方案,但我的代码为字符串(如“icecreamicecream”)提供了错误的输出,其中一些单词在输出中出现了两次。请告诉我哪里出了问题。以下是我的代码:

#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include<string.h>
#define MAX 12
using namespace std;
string arr[]={"i", "like", "sam", "sung", "samsung", "mobile", "ice","cream", "icecream", "man", "go", "mango"};
set<string> dictionary (arr,arr+MAX);
int cnt=0;
void print_words(string str,int i,int j)//i and j denote starting and ending indices respectively of the string to be matched
{
    if(i>j||j>=str.length()||i>=str.length())
        {
        return;
        }
    string temp (str, i, j-i+1);
    if(dictionary.find(temp)==dictionary.end())
        print_words(str,i,j+1);
    else
    {
        cout<<temp<<endl;
        cnt++;
        print_words(str,j+1,j+1);
        print_words(str,i,j+1);
    }

}
int main()
{
    string str;
    cin>>str;
    print_words(str,0,0);
    cout<<cnt<<endl;
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大值12
使用名称空间std;
字符串arr[]={“i”、“like”、“sam”、“sung”、“samsung”、“samsung”、“mobile”、“ice”、“cream”、“ice cream”、“man”、“go”、“mango”};
设置字典(arr、arr+MAX);
int-cnt=0;
void print_words(string str,int i,int j)//i和j分别表示要匹配的字符串的起始索引和结束索引
{
如果(i>j | | j>=str.length()| | i>=str.length())
{
返回;
}
字符串温度(str,i,j-i+1);
if(dictionary.find(temp)=dictionary.end())
打印单词(str,i,j+1);
其他的
{
可能是这样(使用STL和迭代器)

注意:输出是按这种方式排序的,主要是因为值的存储取决于在集合中首先找到的元素(其本身由
集合
在内存中存储其各自值的方式决定)

更新:

已更新以反映自定义排序功能

参考资料:

这里有一个解决方案(不完全是您想要的输出):


您的问题有几个解决方案,但您似乎没有将它们分开(示例解决方案:
冰淇淋冰淇淋
冰淇淋
冰淇淋
冰淇淋
…)。根据我,显示的输出应该是:冰淇淋i冰淇淋冰淇淋您应该在问题中发布您期望/想要的内容(比你的评论更详细)。我已经编辑了这个问题。好的,但是:这不是线性搜索(在“回溯”之前)。通过线性搜索,你将得到
I cream
(注意,
ice
不存在)。@synxix这非常有用。
#include <iostream>
#include <set>
#include <vector>
using namespace std;

//use a comparison function to define a custom ordering
//by which to order the strings based on length instead
//of by character:
struct CompareStrings {
    bool operator() (const string& s1, const string& s2) {
        return s1.size() < s2.size();
    }
};

int main() {
    const char *arr[] = {"i", "like", "sam", "sung", "samsung", "mobile", "ice","cream", "icecream", "man", "go", "mango"};
    size_t arr_size = sizeof(arr)/sizeof(arr[0]);

    //initialize the set with the array and with the custom ordering function:
    set <string, CompareStrings> dictionary (arr, arr+arr_size);
    vector <string> solutions;

    set <string>::iterator it;
    vector <string>::iterator jt;

    string test_string = "icecreamicecream";

    for (it = dictionary.begin(); it != dictionary.end(); ++it) {
        size_t found = test_string.find(*it);

        while (found != string::npos) {
            if (found != string::npos) {
                solutions.push_back(*it);
            }
            found = test_string.find(*it, found+1);
        }
    }

    //iterate over the solutions:
    for (jt = solutions.begin(); jt != solutions.end(); ++jt) {
        cout << *jt << endl;
    }

    return 0;
}
i
i
ice
ice
cream
cream
icecream
icecream
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include<string.h>

using namespace std;
string arr[]={"i", "like", "sam", "sung", "samsung", "mobile", "ice","cream", "icecream", "man", "go", "mango"};
set<string> dictionary (arr,arr+MAX);
int cnt=0;

void search_grow(string str, int i, int j)
{
    if(i > j || j >= str.length() || i >= str.length())
    {
        return;
    }

    string temp(str, i, j - i + 1);
    if(dictionary.find(temp) != dictionary.end())
    {
        std::cout << "[search_grow] " << temp << "\n";
        cnt++;
    }
    search_grow(str, i, j + 1);
}

void search_part(string str)
{
    for(int t = 0; t < str.size(); t++)
        search_grow(str, t, t);
}

int main()
{
    string str;
    cin>>str;
        search_part(str);
    cout<<cnt<<endl;
    return 0;
}
[search_grow] i
[search_grow] ice
[search_grow] icecream
[search_grow] cream
[search_grow] i
[search_grow] ice
[search_grow] icecream
[search_grow] cream
8