[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,请 ...
随机推荐
- System.Windows.Forms.Timer反编译学习
using System; using System.ComponentModel; using System.Globalization; using System.Runtime; using S ...
- javascript笔记——jqGrid 格式化时间列
{ name:'startTime', sortable:false, editable:true, width:300, sorttype:'date', //unformat:startTime, ...
- JSTL截取字符串
引入头文件支持<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> &l ...
- windows下nginx以服务自启动
1,下载最新版的 Windows Service Wrapper 程序,例如:"winsw-1.9-bin.exe" 也可以修改它的名字,例如:myapp.exe 2, 将重命名后 ...
- Uploadify 3.2 not work in IE10
uploadify版本:Uploadify Version 3.2.1 現象:在IE10下uploadify上傳按鈕點擊失效 解決方案:替換uploadify下的js引用包 替換文件下載地址: 下载地 ...
- [OpenXml] Read/Write row/cell from excel
public static void test(){ using (SpreadsheetDocument document = SpreadsheetDocument.Open("test ...
- 为云饰数据库添加Index
Asset Collection: 1. _id_ 2. CategoryId_1_Date_-1 3. CategoryId_1_Id_1 4. CategoryId_1_Name_1 5. Cat ...
- JavaScript笔记(一)
JavaScript组成 EcmaScript:核心部分 作为解释器.几乎没有兼容性问题 DOM:Document Object Model,操作HTML页面的入口.有些操作不兼容. BOM:Brow ...
- 修复ecshop商品重量BUG小数位增至五位
如果ECSHOP商品重量录入为1.499千克,数据库存储值为1.499:如果录入1.499克,存储值为1.显然数据保存有误差,虽然在快递运输中,此误差极小可以忽略不计,但从严谨的角度看,这是不合理的. ...
- PHP的$_SERVER['HTTP_HOST']获取服务器地址功能详解,$_SERVER['HTTP_X_FORWARDED_HOST']
uchome的index文件中的二级域名功能判断,使用了php的$_SERVER['HTTP_HOST'],开始对这个不是很了解,所以百度了一下,发现一篇帖子有点意思,转发过来做个记录. 在php中, ...