最近需要用到分词,无聊写个算法。。。
算法:给定一个字典和一句话,做分词;
Target:输入词典,输出所有可能的分词结果
思路:dfs
加速:首先判断是不是这句话里所有的词在字典中都有(validate)
//
// Wordsplit.cpp
//
// Target: Find all possible splitting of a sentence given a dictionary dict
// Howto: refer to main
//
// Created by Rachel on 14-8-16.
// Copyright (c) 2014年 ZJU. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include "vector"
#include <set>
#include<unordered_set>
using namespace std;
class Wordsplit {
private:
vector<string> list;
bool match(string s, string cur_ele){
int l = cur_ele.length();
if (s.substr(0,l)==cur_ele) {
return true;
}
return false;
}
bool validate(string s, unordered_set<string> &dict){
//1. calculate all alphabets in