[itint5]字符串匹配
用hash来做,目前为止做到最好也是case16超时(20w的规模),即使分桶也超时。注意计算hashcode时,'a'要算成1,否则如果'a'为0,那么"aa"和"a"是一样的。下面是超时的代码:
#define BUCKET 65535
#define ulong long long vector<unordered_set<ulong> > uset(BUCKET);
vector<ulong> pow26(11); ulong hashcode(char *str, int n) {
ulong code = 0;
for (int i = 0; i < n; i++) {
code = code * 26 + str[i] - 'a' + 1;
}
return code;
} // 预处理初始化
void initWithString(char *str) {
int len = 0;
while (str[len] != '\0') {
len++;
}
ulong num = 1;
pow26[0] = 1;
for (int i = 1; i <= 10; i++) {
num *= 26;
pow26[i] = num;
}
for (int l = 1; l <= 10; l++) {
vector<ulong> codes(len);
for (int i = 0; i < len; i++) {
if (i + l <= len) {
ulong code = 0l;
if (i == 0) {
code = hashcode(str+i, l);
codes[i] = code;
} else {
ulong diff = pow26[l-1];
diff *= (str[i-1] - 'a' + 1);
code = (codes[i-1] - diff) * 26 + str[i+l-1] - 'a' + 1;
codes[i] = code;
} int buck = code % BUCKET;
uset[buck].insert(code);
}
}
}
}
// 如果query是str的字串,返回true,否则返回false
bool existSubString(char *query) {
int len = strlen(query);
ulong code = hashcode(query, len);
int buck = code % BUCKET;
if (uset[buck].find(code) != uset[buck].end()) {
return true;
} else {
return false;
}
}
如果只存长度为10的字符串到排序好的vector里,然后用二分来做,是能过的。注意有的源字符串长度就小于10了。其他的备选方法还有trie以及后缀数组。
vector<string> vec; // 预处理初始化
void initWithString(char *str) {
set<string> sset;
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (i + 10 >= len) {
string sub(str+i);
sset.insert(sub);
} else {
string sub(str+i, str+i+10);
sset.insert(sub);
}
} for (set<string>::iterator it = sset.begin(); it != sset.end(); it++) {
vec.push_back(*it);
}
}
// 如果query是str的字串,返回true,否则返回false
bool existSubString(char *query) {
string str(query);
int low = 0;
int high = vec.size()-1; while (low <= high) {
int mid = (low + high) / 2;
bool found = true;
for (int i = 0; i < str.length(); i++) {
if (vec[mid][i] < str[i]) {
low = mid + 1;
found = false;
break;
} else if (vec[mid][i] > str[i]) {
high = mid - 1;
found = false;
break;
}
}
if (found) return true;
}
return false;
}
[itint5]字符串匹配的更多相关文章
- 字符串匹配的KMP算法
~~~摘录 来源:阮一峰~~~ 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串”BBC ABCDAB ABCDABCDABDE”,我想知道,里面是否包含另一个字符串”ABCDABD”? 许 ...
- {Reship}{KMP字符串匹配}
关于KMP字符串匹配的介绍和归纳,作者的思路非常清晰,推荐看一下 http://blog.csdn.net/v_july_v/article/details/7041827
- 字符串匹配(hash算法)
hash函数对大家来说不陌生吧 ? 而这次我们就用hash函数来实现字符串匹配. 首先我们会想一下二进制数. 对于任意一个二进制数,我们将它化为10进制的数的方法如下(以二进制数1101101为例): ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- sdut 2125串结构练习--字符串匹配【两种KMP算法】
串结构练习——字符串匹配 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目链接:http://acm.sdut.edu.cn/sduto ...
- C语言字符串匹配函数
C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...
- 字符串匹配--Karp-Rabin算法
主要特征 1.使用hash函数 2.预处理阶段时间复杂度O(m),常量空间 3.查找阶段时间复杂度O(mn) 4.期望运行时间:O(n+m) 本文地址:http://www.cnblogs.com/a ...
- 字符串匹配的KMP算法详解及C#实现
字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
随机推荐
- Poj 2586 / OpenJudge 2586 Y2K Accounting Bug
1.Link: http://poj.org/problem?id=2586 2.Content: Y2K Accounting Bug Time Limit: 1000MS Memory Lim ...
- 【实习记】2014-08-19升级vim配置YouCompleteMe并debug的过程+qtcreator有语言包没法换语言
做了个小项目,有空闲可以做点事了. 偶然查资料看见YouCompleteMe的鼎鼎大名. 演示demo <img src="http://i.imgur.com/0OP4ood ...
- JavaScript jQuery 入门回顾
$符号 $是著名的jQuery符号.实际上,jQuery把所有功能全部封装在一个全局变量jQuery中,而$也是一个合法的变量名,它是变量jQuery的别名: window.jQuery; // j ...
- 无法建立到http://localhost:6080/arcgis/manager/的连接
安装ArcGIS server10.1后,打开管理页面提示“无法建立到http://localhost:6080/arcgis/manager/的连接” 原因是:在ArcGIS for Server ...
- DTcms 扩展字段标签调用
前台模版: 文章列表:{dr[author]} 文章内容{model.fields[author]} 点击数 后台CS文件:model.fields["author"].ToStr ...
- 使用urllib2的HttpResponse导致内存不回收(内存泄漏)
问题出现环境:python 2.7.1(X)及以下, Windows(或CentOS) 这个问题产生在lib/urllib2.py的line 1174 (python 2.7.1),导致形成了cycl ...
- Mysql grant权限管理
MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 [identified by '密码'] 最常用的,弄主从同步的时候,给从库的slave用户设置拥有所 ...
- Eclipse 中隐藏的 5 个非常有用的功能
Eclipse就是一头野兽.它也是一套设备,神秘但更具威力.有些人称它为一个持续变形机.另一些人则称它是一个变异体.不错,它很庞大,需要花费多年才能掌握.而在你好不容易掌握之后,你的老板出现了然后告诉 ...
- go语言使用redis —— redigo
redis的client有好多好多,go语言的client在redis官方有两个推荐,radix和redigo.选择哪一个好呢?确实很纠结,后来掷硬币决定选择redigo了. redis.go.red ...
- Mac OS X 10.10 Yosemite PHP 5.5.14 free type support fix
通过将php将至5.4来勉强支持freetype扩展,不推荐此方法 after upgrading to new Mac OS X Yosemite, i noticed that free type ...