hdu5972_Regular Number_快速匹配+bitset
来源:互联网 发布:代理商授权书php源码 编辑:程序博客网 时间:2024/06/05 00:28
Regular Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 789 Accepted Submission(s): 181
Problem Description
Using regular expression to define a numeric string is a very common thing. Generally, use the shape as follows:
(0|9|7) (5|6) (2) (4|5)
Above regular expression matches 4 digits:The first is one of 0,9 and 7. The second is one of 5 and 6. The third is 2. And the fourth is one of 4 and 5. The above regular expression can be successfully matched to 0525, but it cannot be matched to 9634.
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.
(0|9|7) (5|6) (2) (4|5)
Above regular expression matches 4 digits:The first is one of 0,9 and 7. The second is one of 5 and 6. The third is 2. And the fourth is one of 4 and 5. The above regular expression can be successfully matched to 0525, but it cannot be matched to 9634.
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 (1 ≤ N ≤ 1000),on behalf of the regular representation of the N bit string.In the next N lines,the first integer of the i-th line isai(1≤ai≤10) ,representing that the i-th position of regular expression has ai numbers to be selected.Next there are ai numeric characters. In the last line,there is a numeric string.The length of the string is not more than 5 * 10^6.
Output
Output all substrings that can be matched by the regular expression. Each substring occupies one line
Sample Input
43 0 9 72 5 72 2 52 4 509755420524
Sample Output
975575540524
题意:
给你N位数,接下来有N行,第i行先输入n,表示这个数的第i 位上可以在接下来的n个数中挑选,然后i 行再输n个数。
然后输入需要匹配的母串,让你输出母串中有多少个可行的N位子串。
解:
这题首先没法使用KMP,因为在匹配失败后没法返回到准确的位置。
然后在网上向别人学了下代码,才明白这题bitset的巧妙的运用。
关于bitset的用法:http://blog.csdn.net/no2015214099/article/details/72902794
这题 bitset 的使用相当于是作为一个指针来使用的。
首先用bitset定义出现的数会在哪几位上出现,置为1。
定义ans的初始位为1,每一次母串对应位与该位出现的数的bitset进行与比较(表明该位上是否能出现该数)。因为一旦失败则置0,因此如果1出现在ans的第n位上则表明之前的n-1位全部匹配成功。
此题,使用bitset的复杂度为O(n*len/x)(len为母串长,x为机器码长)。
此题必须使用puts,gets进行输入输出,不然会超时。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>#include <bitset>using namespace std;const int maxn=1e6+50;char str[5*maxn];bitset<1005> s[20];bitset<1005> ans;int main(){ int n; while(~scanf("%d",&n)) { for(int i=0;i<20;i++) s[i].reset(); ans.reset(); for(int i=0;i<n;i++) { int N,temp; scanf("%d",&N); for(int j=0;j<N;j++) { scanf("%d",&temp); s[temp].set(i); } } getchar(); gets(str); int len=strlen(str); for(int i=0;i<len;i++) { ans=ans<<1; ans[0]=1; ans&=s[str[i]-'0']; if(ans[n-1]==1) { char temp=str[i+1]; str[i+1]='\0'; puts(str+i-n+1); str[i+1]=temp; } } } return 0;}
阅读全文
0 0
- hdu5972_Regular Number_快速匹配+bitset
- hdu5972 Regular Number (bitset应用+快速匹配+shiftAnd匹配算法学习模板)
- hdu5972 快速匹配bitset优化(shiftand算法)2016ACM/ICPC亚洲区大连站
- bitset
- bitset
- BitSet
- bitset
- bitset
- BitSet
- bitset
- bitset
- bitset
- bitset
- bitset
- BitSet
- bitset
- bitset
- Bitset
- 了解VUE的render函数
- Android Studio 设置代码页的背景图片
- 进程线程简介
- maven--私服的搭建(Nexus的使用)
- 微信小程序 三级联动省市区
- hdu5972_Regular Number_快速匹配+bitset
- python安装virtualenvwrapper报错解决办法
- 点在任意直线上的投影点
- JavaSE 异常处理
- SpringMVC返回json数据的三种方式
- usb免驱动之UVC
- dubbo学习资源
- 数据结构:队列
- ERROR与EXCEPTION的区别