编程算法 - 字典分词 代码(C)
字典分词 代码(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)的更多相关文章
- 编程算法 - 分割数 代码(C)
分割数 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n个无差别的物品, 将它们划分成不超过m组, 求出划分方法数模M的余数. 比如: n= ...
- 编程算法 - 数丑陋 代码(C)
数丑陋 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 我们把仅仅包括因子2, 3 和 5的数称作丑数. 求按从小到大的顺序的第5个丑数. 能够 ...
- 编程算法 - 区间调度问题 代码(C)
区间调度问题 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n项工作, 每项工作分别在s时间開始, 在t时间结束. 对于每项工作能够选择參与 ...
- 编程算法 - 切割排序 代码(C)
切割排序 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 排序切割, 把一个数组分为, 大于k\小于k\等于k的三个部分. 能够使用高速排序的Parti ...
- 编程算法 - 二部图确定 代码(C)
二部图确定 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定一个具有n个顶点的图. 要给图上每一个顶点染色, 而且要使相邻的顶点颜色不同. ...
- 编程算法 - 全然背包问题 代码(C)
全然背包问题 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n个重量和价值分别为w,v的物品, 从这些物品中挑选出总重量不超过W的物品, 求 ...
- 编程算法 - 远征队(expedition) 代码(C)
远征队(expedition) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 远征队有一辆卡车须要行驶L单位的距离, 開始时, 车上有P单位的 ...
- 游戏编程算法与技巧 Game Programming Algorithms and Techniques (Sanjay Madhav 著)
http://gamealgorithms.net 第1章 游戏编程概述 (已看) 第2章 2D图形 (已看) 第3章 游戏中的线性代数 (已看) 第4章 3D图形 (已看) 第5章 游戏输入 (已看 ...
- 对一致性Hash算法,Java代码实现的深入研究
一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...
随机推荐
- 【c语言】统计一个数二进制中的1的个数
// 统计一个数二进制中的1的个数 #include <stdio.h> int count(int a) { int count = 0; while (a) { count++; a ...
- .Net 配置文件——继承ConfigurationSection实现自己定义处理类处理自己定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自己定义节点的类.还能够通过继承ConfigurationSection类实现相同效果. 首先说下.Net配置文件里一 ...
- 手动配置S2SH三大框架报错(一)
十二月 08, 2013 9:24:51 下午 org.apache.catalina.core.AprLifecycleListener init 严重: An incompatible versi ...
- 与众不同 windows phone (15) - Media(媒体)之后台播放音频
原文:与众不同 windows phone (15) - Media(媒体)之后台播放音频 [索引页][源码下载] 与众不同 windows phone (15) - Media(媒体)之后台播放音频 ...
- [页面模板框架对比] Apache Tiles VS Sitemesh
1. 原理对比 (1) Apache Tiles 顾名思义,Tile是瓷砖的意思,也就是说一个网页是由多个Tile组成的. 用户通过访问一个页面的Apache Tiles定义名,就可以访问一个由定义文 ...
- [UVALive 6663 Count the Regions] (dfs + 离散化)
链接:https://icpcarchive.ecs.baylor.edu/index.php? option=com_onlinejudge&Itemid=8&page=show_p ...
- POJ1422 Air Raid 【DAG最小路径覆盖】
Air Raid Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6763 Accepted: 4034 Descript ...
- cocos2d/-x 用CCRenderTexture为一个CCLabelTTF创建阴影。
游戏UI中为了使字体更加漂亮,通常需要为字体添加一个阴影.其实不用美工,程序就可以添加.先为CCLabelTTF创建一个CCRenderTexture: CCRenderTexture* CCLabe ...
- Shell编程中Shift的用法(转)
位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1. 非常 ...
- 【图像处理】Bilinear Image Scaling
Bilinear image scaling is about the same as nearest neighbor image scaling except with interpolation ...