Regular Number 字符串匹配算法 Shift_and
Using regular expression to define a numeric string is a very common thing. Generally, use the shape as follows:
(||) (|) () (|)
Above regular expression matches digits:The first is one of , and . The second is one of and . The third is . And the fourth is one of and . The above regular expression can be successfully matched to , but it cannot be matched to .
Now,giving you a regular expression like the above formula,and a long string of numbers,please find out all the substrings of this long string that can be matched to the regular expression.
Input
It contains a set of test data.The first line is a positive integer N ( ≤ N ≤ ),on behalf of the regular representation of the N bit string.In the next N lines,the first integer of the i-th line is ai(≤ai≤)ai(≤ai≤),representing that the i-th position of regular expression has aiai numbers to be selected.Next there are aiai numeric characters. In the last line,there is a numeric string.The length of the string is not more than * ^.
Output
Output all substrings that can be matched by the regular expression. Each substring occupies one line
Sample Input Sample Output
适用于t[]串长度较小的情况,利用位运算一般比KMP算法快两倍以上。
用D来记录前缀的匹配情况,要使用Shift 算法,需要一个辅助表B。B 是一个字典,key 是问题域字符集中的每个字符,value 是一个n 位无符号整数,记录该字符在模式串T 的哪些位置出现。
由于D【j】表示的是T[0..J]是否是S[0...i]的后缀,所以只有当D[j-1]==1而且S[i]==T[j]的情况下,D[j]才等于1,同时将最低位设置为1,这样产生从当前位作为第一位的解。
,Shift-And 算法实现
Shift-And 匹配过程代码:
![]() |
由于位运算在计算机中可以并行进行,每次循环的执行是常数时间的,所以上面代码段的复杂度是 O(m)。
3,辅助表 B
上面没有提到如何得到辅助表B。很简单,只要获得模式串T 中每个字符出现的位置。
![]() |
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<list>
#include<bitset>
#include<string>
#include<functional> using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MAXN = 5e6 + ;
#define L 1009
#define INF 1000000009
#define eps 0.00000001
#define MOD 1000
bitset<> B[], D;
char str[MAXN];
int main()
{
int n, tmp, t;
scanf("%d", &n);
for (int i = ; i < n; i++)
{
scanf("%d", &tmp);
while (tmp--)
{
scanf("%d", &t);
B[t].set(i);
}
}
getchar();
gets(str);
int l = strlen(str);
for (int i = ; i < l; i++)
{
D = (D << ).set()&B[str[i] - ''];
if (D[n - ])
{
char ch = str[i + ];
str[i + ] = '\0';
puts(str + i - n + );
str[i + ] = ch;
}
}
}
Regular Number 字符串匹配算法 Shift_and的更多相关文章
- hdu 5972 Regular Number 字符串Shift-And算法 + bitset
题目链接 题意 给定两个串\(S,T\),找出\(S\)中所有与\(T\)匹配的子串. 这里,\(T\)的每位上可以有若干(\(\leq 10\))种选择,匹配的含义是:对于\(S\)的子串的每一位, ...
- HDU 5972 Regular Number
Regular Number http://acm.hdu.edu.cn/showproblem.php?pid=5972 题意: 给定一个字符串,求多少子串满足,子串的第i位,只能是给定的数(小于等 ...
- 字符串匹配算法 - KMP
前几日在微博上看到一则微博是说面试的时候让面试者写一个很简单的字符串匹配都写不出来,于是我就自己去试了一把.结果写出来的是一个最简单粗暴的算法.这里重新学习了一下几个经典的字符串匹配算法,写篇文章以巩 ...
- Boyer-Moore 字符串匹配算法
字符串匹配问题的形式定义: 文本(Text)是一个长度为 n 的数组 T[1..n]: 模式(Pattern)是一个长度为 m 且 m≤n 的数组 P[1..m]: T 和 P 中的元素都属于有限的字 ...
- KMP单模快速字符串匹配算法
KMP算法是由Knuth,Morris,Pratt共同提出的算法,专门用来解决模式串的匹配,无论目标序列和模式串是什么样子的,都可以在线性时间内完成,而且也不会发生退化,是一个非常优秀的算法,时间复杂 ...
- 字符串匹配算法之BF(Brute-Force)算法
BF(Brute-Force)算法 蛮力搜索,比较简单的一种字符串匹配算法,在处理简单的数据时候就可以用这种算法,完全匹配,就是速度慢啊. 基本思想 从目标串s 的第一个字符起和模式串t的第一个字符进 ...
- 【原创】通俗易懂的讲解KMP算法(字符串匹配算法)及代码实现
一.本文简介 本文的目的是简单明了的讲解KMP算法的思想及实现过程. 网上的文章的确有些杂乱,有的过浅,有的太深,希望本文对初学者是非常友好的. 其实KMP算法有一些改良版,这些是在理解KMP核心思想 ...
- 字符串匹配算法——KMP算法学习
KMP算法是用来解决字符串的匹配问题的,即在字符串S中寻找字符串P.形式定义:假设存在长度为n的字符数组S[0...n-1],长度为m的字符数组P[0...m-1],是否存在i,使得SiSi+1... ...
- 4种字符串匹配算法:KMP(下)
回顾:4种字符串匹配算法:BS朴素 Rabin-karp(上) 4种字符串匹配算法:有限自动机(中) 1.图解 KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R ...
随机推荐
- mac更改本地mysql登陆密码
安装完mysql 之后,登陆以后,运行任何命令,提示 You must SET PASSWORD before executing this statement解决办法. step 1: SET PA ...
- [Usaco2015 Jan]Moovie Mooving
Description Bessie is out at the movies. Being mischievous as always, she has decided to hide from F ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)
题目描述 We consider a positive integer perfect, if and only if it is equal to the sum of its positive d ...
- 题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- EditText(6)EditText的子类
1.Known Direct Subclasses AppCompatEditText, AutoCompleteTextView, ExtractEditText, GuidedActionEd ...
- Kali Linux的介绍
Kali Linux是什么? Kali Linux是一个渗透测试平台兼安全审计平台,它集成了多款漏洞检测.目标识别和漏洞利用工具. Kali Linux是专门用于渗透测试的Linux操作系统:基于De ...
- Ajax动态加载数据
前言: 1.这个随笔实现了一个Ajax动态加载的例子. 2.使用.net 的MVC框架实现. 3.这个例子重点在前后台交互,其它略写. 开始: 1.控制器ActionResult代码(用于显示页面) ...
- 3星|《投机教父尼德霍夫的股票投机术》:2003年的书了。作者97年投机大亏后在CNBC《金钱》栏目上的股市评论文章集。
查资料作者在97年金融危机中大亏,之后在CNBC<金钱>栏目上跟人合写股市评论文章,本书是那些股评文章的集合.有资料说作者在08年有一次大亏. 从这些文章看,作者是比较冷静地看待股市的,不 ...
- HDU_3172_带权并查集
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- CAD创建组(网页版)
主要用到函数说明: _DMxDrawX::CreateGroup 创建组.如果组名已经存在,就把实体加入组中.详细说明如下: 参数 说明 BSTR pszName 组名.,如果为空,创建匿名组 IDi ...