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的更多相关文章

  1. hdu 5972 Regular Number 字符串Shift-And算法 + bitset

    题目链接 题意 给定两个串\(S,T\),找出\(S\)中所有与\(T\)匹配的子串. 这里,\(T\)的每位上可以有若干(\(\leq 10\))种选择,匹配的含义是:对于\(S\)的子串的每一位, ...

  2. HDU 5972 Regular Number

    Regular Number http://acm.hdu.edu.cn/showproblem.php?pid=5972 题意: 给定一个字符串,求多少子串满足,子串的第i位,只能是给定的数(小于等 ...

  3. 字符串匹配算法 - KMP

    前几日在微博上看到一则微博是说面试的时候让面试者写一个很简单的字符串匹配都写不出来,于是我就自己去试了一把.结果写出来的是一个最简单粗暴的算法.这里重新学习了一下几个经典的字符串匹配算法,写篇文章以巩 ...

  4. Boyer-Moore 字符串匹配算法

    字符串匹配问题的形式定义: 文本(Text)是一个长度为 n 的数组 T[1..n]: 模式(Pattern)是一个长度为 m 且 m≤n 的数组 P[1..m]: T 和 P 中的元素都属于有限的字 ...

  5. KMP单模快速字符串匹配算法

    KMP算法是由Knuth,Morris,Pratt共同提出的算法,专门用来解决模式串的匹配,无论目标序列和模式串是什么样子的,都可以在线性时间内完成,而且也不会发生退化,是一个非常优秀的算法,时间复杂 ...

  6. 字符串匹配算法之BF(Brute-Force)算法

    BF(Brute-Force)算法 蛮力搜索,比较简单的一种字符串匹配算法,在处理简单的数据时候就可以用这种算法,完全匹配,就是速度慢啊. 基本思想 从目标串s 的第一个字符起和模式串t的第一个字符进 ...

  7. 【原创】通俗易懂的讲解KMP算法(字符串匹配算法)及代码实现

    一.本文简介 本文的目的是简单明了的讲解KMP算法的思想及实现过程. 网上的文章的确有些杂乱,有的过浅,有的太深,希望本文对初学者是非常友好的. 其实KMP算法有一些改良版,这些是在理解KMP核心思想 ...

  8. 字符串匹配算法——KMP算法学习

    KMP算法是用来解决字符串的匹配问题的,即在字符串S中寻找字符串P.形式定义:假设存在长度为n的字符数组S[0...n-1],长度为m的字符数组P[0...m-1],是否存在i,使得SiSi+1... ...

  9. 4种字符串匹配算法:KMP(下)

    回顾:4种字符串匹配算法:BS朴素 Rabin-karp(上) 4种字符串匹配算法:有限自动机(中) 1.图解 KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R ...

随机推荐

  1. 2017杭电多校06Rikka with Graph

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. ASP.NET GridView 控件绑定 CheckBoxList

    需求:设计这样一个页面,在页面上可以自由选择和展示各省份下城市? 思路:一次性查询出所需的记录(查询数据库的操作不宜写到 C# 代码的循环语句中),并保存到全局变量中,之后根据条件过滤出需要的.可以在 ...

  3. pycharm但多行注释快捷键

    pycharm中同时注释多行代码快捷键: 代码选中的条件下,同时按住 Ctrl+/,被选中行被注释,再次按下Ctrl+/,注释被取消

  4. C# 输出控制台结果到文件

    StreamWriter sw = new StreamWriter(@"c:\output.txt"); Console.SetOut(sw); Console.WriteLin ...

  5. Docker (1) 基本概念和安装

    Docker简介 什么是容器? 一种虚拟化的方案,操作系统级别的虚拟化.容器是一个轻量的.独立的.可执行的包,包含了执行它所需要的所有东西:代码.运行环境.系统工具.系统库.设置.很长一段时间中,容器 ...

  6. VC++常见错误原因解析--error LNK2019: 无法解析的外部符号 "public: void __thiscall

    根据个人遇到这个错误时的记录,原因可以分为一下几种: 原因一: 只是在.h里面声明了某个方法, 没有在cpp里面实现 . 具体讲,有时候在头文件中声明了需要的方法,确实忘记了在源文件中实现: 有时候在 ...

  7. CentOS上oracle 11g R2数据库安装折腾记

    1.虚拟机上centos镜像的获取.这里推荐网易镜像站中的CentOS7版本(其他开源镜像站亦可).这里给出链接: http://mirrors.163.com/centos/7.3.1611/iso ...

  8. 【百度编辑器ueditor】工具,如何去掉百度编辑器 ueditor 元素路径、字数统计等

    去掉如下截图: 在百度编辑器 ueditor 根目录下: ueditor.config.js 文件中 搜索并将参数elementPathEnabled设置成false即可 常用功能开关如下: ,ele ...

  9. iOS的影片播放 MediaPlayer 和 AVPlayer

    在iOS開發上,如果遇到需要播放影片,如開機動畫…,我們很習慣地會使用MediaPlayer來播放影片,因為很方便使用,所以就一直使用下去.但是隨著客戶的要求越來越嚴苛,尤其是過場動畫或互動效果上的表 ...

  10. C++/C union使用记一下锅

    //首先,学习编程一定要记得加几个群或者加几个讨论组,因为这样你才能不断地进步还有吵架/滑稽 记一下 关于使用union结构体时遇到的一些坑 To zero-initialize an object ...