字典分词 代码(C)

本文地址: http://blog.csdn.net/caroline_wendy

给定字典, 给定一句话, 进行分词.

使用深度遍历(DFS)的方法.

使用一个參数string, 保存当前分支的分词后的句子; 使用一个參数vector, 保存全部可能的组合.

使用一个验证函数, 推断句子能否够分词.

代码:

/*
* main.cpp
*
* Created on: 2014.9.18
* Author: Spike
* Copyright (c) 2014年 WCL. All rights reserved.
*/ /*eclipse cdt, gcc 4.8.1*/ #include <iostream>
#include <vector>
#include <string>
#include <set> using namespace std; bool Match(string s, string m) {
int l = m.length();
if (s.substr(0, l) == m) {
return true;
}
return false;
} bool Validate(string s, vector<string> &dict) {
//1. calculate all alphabets in the query
set<char> sc;
for (size_t i = 0; i < s.length(); i++) {
sc.insert(s[i]);
}
//2. calculate all alphabets in the dictionary
set<char> dc;
for (vector<string>::iterator it = dict.begin();
it != dict.end(); it++)
{
for (size_t i = 0; i < (*it).length(); i++) {
dc.insert((*it)[i]);
}
}
for (set<char>::iterator it = sc.begin(); it != sc.end(); it++) {
if (dc.find(*it) == dc.end()) {
return false;
}
}
return true;
} string Split(string s, vector<string> &dict, string cur, vector<string>& list) {
if (s.length() == 0) {
list.push_back(cur);
return s;
}
for (vector<string>::iterator it = dict.begin(); it != dict.end(); it++) {
if (Match(s, *it)) {
string tmp = cur;
string latter = s.substr(it->length(), s.length() - it->length());
cur += (*it) + "~"; // add current word to cur_str
cur += Split(latter, dict, cur, list); // split remaining words
cur = tmp; //back to last status
}
}
return "No Result";
} vector<string> SplitWords(string s, vector<string> &dict) {
string cur = "";
vector<string> list;
if (!Validate(s, dict)) {
return list;
}
Split(s, dict, cur, list);
return list;
} int main()
{
vector<string> dict={"程序猿","公务员","员","我","喜","做","程序","一","欢","喜欢","做一个","一个"};
vector<string> words = SplitWords("我喜欢做一个程序猿", dict);
for (vector<string>::iterator it=words.begin(); it!=words.end(); it++) {
cout<<(*it)<<endl;
}
return 0;
}

简化版本号(没有验证):

/*
* main.cpp
*
* Created on: 2014.9.18
* Author: Spike
* Copyright (c) 2014年 WCL. All rights reserved.
*/ /*eclipse cdt, gcc 4.8.1*/ #include <iostream>
#include <vector>
#include <string>
#include <set> using namespace std; bool Match(string s, string m) {
int l = m.length();
if (s.substr(0, l) == m) {
return true;
}
return false;
} string Split(string s, vector<string> &dict, string cur, vector<string>& list) {
if (s.length() == 0) {
list.push_back(cur);
return s;
} for (vector<string>::iterator it = dict.begin(); it != dict.end(); it++) {
if (Match(s, *it)) {
string tmp = cur;
string latter = s.substr(it->length());
cur += (*it) + " | "; // add current word to cur_str
cur += Split(latter, dict, cur, list); // split remaining words
cur = tmp; //back to last status
}
} return "No Result";
} vector<string> SplitWords(string s, vector<string> &dict) {
string cur = "";
vector<string> list;
Split(s, dict, cur, list);
return list;
} int main()
{
vector<string> dict={"程序猿","公务员","员","我","喜","做","程序","一","欢","喜欢","做一个","一个"};
string s = "我喜欢做一个程序猿";
vector<string> words = SplitWords(s, dict);
for (vector<string>::iterator it=words.begin(); it!=words.end(); it++) {
cout<<(*it)<<endl;
}
return 0;
}

输出:

我~喜~欢~做~一个~程序猿~
我~喜~欢~做~一个~程序~员~
我~喜~欢~做一个~程序猿~
我~喜~欢~做一个~程序~员~
我~喜欢~做~一个~程序猿~
我~喜欢~做~一个~程序~员~
我~喜欢~做一个~程序猿~
我~喜欢~做一个~程序~员~

编程算法 - 字典分词 代码(C)的更多相关文章

  1. 编程算法 - 分割数 代码(C)

    分割数 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n个无差别的物品, 将它们划分成不超过m组, 求出划分方法数模M的余数. 比如: n= ...

  2. 编程算法 - 数丑陋 代码(C)

    数丑陋 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 我们把仅仅包括因子2, 3 和 5的数称作丑数. 求按从小到大的顺序的第5个丑数. 能够 ...

  3. 编程算法 - 区间调度问题 代码(C)

    区间调度问题 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n项工作, 每项工作分别在s时间開始, 在t时间结束. 对于每项工作能够选择參与 ...

  4. 编程算法 - 切割排序 代码(C)

    切割排序 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 排序切割, 把一个数组分为, 大于k\小于k\等于k的三个部分. 能够使用高速排序的Parti ...

  5. 编程算法 - 二部图确定 代码(C)

    二部图确定 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定一个具有n个顶点的图. 要给图上每一个顶点染色, 而且要使相邻的顶点颜色不同.  ...

  6. 编程算法 - 全然背包问题 代码(C)

    全然背包问题 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n个重量和价值分别为w,v的物品, 从这些物品中挑选出总重量不超过W的物品, 求 ...

  7. 编程算法 - 远征队(expedition) 代码(C)

    远征队(expedition) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 远征队有一辆卡车须要行驶L单位的距离, 開始时, 车上有P单位的 ...

  8. 游戏编程算法与技巧 Game Programming Algorithms and Techniques (Sanjay Madhav 著)

    http://gamealgorithms.net 第1章 游戏编程概述 (已看) 第2章 2D图形 (已看) 第3章 游戏中的线性代数 (已看) 第4章 3D图形 (已看) 第5章 游戏输入 (已看 ...

  9. 对一致性Hash算法,Java代码实现的深入研究

    一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...

随机推荐

  1. hbase memstorelab

    关于MemStore的补充 在通过HStore.add向store中加入�一个kv时,首先把数据写入到memstore中.这一点没有什么说明: publiclongadd(finalKeyValue ...

  2. Spring Boot Mongodb

    Spring注解学习,有助于更好的理解下面代码: @ConditionOnClass表明该@Configuration仅仅在一定条件下才会被加载,这里的条件是Mongo.class位于类路径上 @En ...

  3. HDU3714 Error Curves (单峰函数)

    大意: 给你n个二次函数Si(x),F(x) = max{Si(x)} 求F(x)在[0,1000]上的最小值. S(x)=ax^2+bx+c       (0<=a<=100, |b|, ...

  4. HDU1849 Rabbit and Grass()

    用异或看取得的值是否为0推断 思想换没搞懂 #include<stdio.h> int main() { int ans,n,a; while(scanf("%d",& ...

  5. Swift - 操作表(UIActionSheel)的用法,也叫底部警告框

    1,下面创建一个操作表(或叫底部警告框)并弹出显示 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class ViewController: UIViewC ...

  6. uva 1415 - Gauss Prime(高斯素数)

    题目链接:uva 1415 - Gauss Prime 题目大意:给出一个a,b,表示高斯数a+bi(i=−2‾‾‾√,推断该数是否为高斯素数. 解题思路: a = 0 时.肯定不是高斯素数 a != ...

  7. IOT和HEAP表区别

    Index Organized table by itself is a B-tree index. Index key is the primary key and the rest of colu ...

  8. Missile:双状态DP

    题目 描写叙述 Long , long ago ,country A invented a missile system to destroy the missiles from their enem ...

  9. 呜呼!Node.js是什么?

    近期看到非常多站点都使用node.js.開始感到非常好奇.就開始推測这是个什么东西,大概就是个js文件吧,所以開始根本 没有在意,可是越感觉就认为越不正确劲,为什么大家都在用它呢?所以我决定搞个明确. ...

  10. frontend http 前端名字定义问题

    https://www.winfae.com/admin/api/menu haproxy 日志: Jun 24 13:04:49 localhost haproxy[14817]: 115.236. ...