Regular Number 字符串匹配算法 Shift_and
2024-09-08 16:41:28
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;
}
}
}
最新文章
- C数组下标越界
- Java设计模式之-----工厂模式(简单工厂,抽象工厂)
- [CentOS] 打造vim环境
- 利用css中的border生成三角,兼容包括IE6的主流浏览器
- Microsoft Robotics Developer Studio 4
- Ext.grid.Panel表格分页
- .net mvc4 从客户端中检测到有潜在危险的 Request.Form 值
- php多语言切换---转载
- angular4在prod模式下的JIT编译问题
- Mysql索引介绍及常见索引的区别
- 利用QrCode.Net生成二维码 asp.net mvc c#
- SpringBoot整合SpringCloud搭建分布式应用
- MySQL5.7 并行复制的学习
- cv2.findContours
- WinEdt和LaTeX的简介
- CSAPP:第三章程序的机器级表示3
- 改变select箭头样式
- Mac 安装多个python环境
- Python 类方法
- c++ 模板参数做容器参数迭代器报错 vector<;T>;::const_iterator,typename const报错