hdu 5972 Regular Number 字符串Shift-And算法 + bitset
题目链接
题意
给定两个串\(S,T\),找出\(S\)中所有与\(T\)匹配的子串。
这里,\(T\)的每位上可以有若干(\(\leq 10\))种选择,匹配的含义是:对于\(S\)的子串的每一位,\(T\)的相应位都有一种选择与之对应。
题解
shift-and算法详解 https://www.douban.com/note/321872072/
搜出来的题解全都是\(shift-and\)的...。
学习了一波...然而并不明白\(kmp\)为什么不可以...。
看到这道题直觉就是和hdu 4749一样美滋滋写了然后\(T\)了...。
Code
#include <bits/stdc++.h>
#define maxn 1010
#define maxm 5000010
using namespace std;
typedef long long LL;
char s[maxm];
bitset<maxn> B[11];
const int BUF_SIZE = (int)1e4+10;
namespace fastIO{
#define BUF_SIZE 100000
#define OUT_SIZE 1000000
bool IOerror=0;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend){
p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
if (pend==p1){IOerror=1;return -1;}
}return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline int read(char *s){
char ch=nc();
for(;blank(ch);ch=nc());
if(IOerror)return 0;
for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
*s=0;
return 1;
}
inline int RI(int &a){
char ch=nc(); a=0;
for(;blank(ch);ch=nc());
if(IOerror)return 0;
for(;!blank(ch)&&!IOerror;ch=nc())a=a*10+ch-'0';
return 1;
}
struct Ostream_fwrite{
char *buf,*p1,*pend;
Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}
void out(char ch){
if (p1==pend){
fwrite(buf,1,BUF_SIZE,stdout);p1=buf;
}*p1++=ch;
}
void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}}
~Ostream_fwrite(){flush();}
}Ostream;
inline void print(char x){Ostream.out(x);}
inline void println(){Ostream.out('\n');}
inline void flush(){Ostream.flush();}
char Out[OUT_SIZE],*o=Out;
inline void print1(char c){*o++=c;}
inline void println1(){*o++='\n';}
inline void flush1(){if (o!=Out){if (*(o-1)=='\n')*--o=0;puts(Out);}}
struct puts_write{
~puts_write(){flush1();}
}_puts;
};
int n, cnt[maxn], a[maxn][11];
void GetDict() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < cnt[i]; ++j) {
B[a[i][j]][i] = 1;
}
}
}
void work() {
for (int i = 0; i < n; ++i) {
fastIO::RI(cnt[i]);
for (int j = 0; j < cnt[i]; ++j) fastIO::RI(a[i][j]);
}
GetDict();
fastIO::read(s);
int len = strlen(s);
bitset<maxn> D;
for (int i = 0; i < len; ++i) {
D <<= 1; D[0] = 1;
D &= B[s[i]-'0'];
if (D[n-1]) {
char temp = s[i+1]; s[i+1] = '\0';
printf("%s\n", s+i-n+1);
s[i+1] = temp;
}
}
}
int main() {
while (fastIO::RI(n)) work();
return 0;
}
TLE Code kmp Ver.
#include <bits/stdc++.h>
#define maxn 1010
#define maxm 5000010
char s[maxm];
int b[maxm];
int a[maxn][11], cnt[maxn], f[maxn];
using namespace std;
typedef long long LL;
int n, m;
bool match1(int x, int y) {
for (int i = 0; i < cnt[x]; ++i) {
for (int j = 0; j < cnt[y]; ++j) {
if (a[x][i] == a[y][j]) return true;
}
}
return false;
}
void getfail() {
f[0] = f[1] = 0;
for (int i = 1; i < n; ++i) {
int j = f[i];
while (j && !match1(i, j)) j = f[j];
f[i+1] = match1(i, j) ? j+1 : 0;
}
}
bool match2(int x, int y) {
for (int i = 0; i < cnt[y]; ++i) {
if (b[x] == a[y][i]) return true;
}
return false;
}
void kmp() {
int j = f[1];
for (int i = 1; i < m; ++i) {
while (j && !match2(i, j)) j = f[j];
if (match2(i, j)) ++j;
if (j == n) {
for (int k = i-n+1; k <= i; ++k) putchar('0'+b[k]);
putchar('\n');
}
}
}
void work() {
for (int i = 0; i < n; ++i) {
scanf("%d", &cnt[i]);
for (int j = 0; j < cnt[i]; ++j) scanf("%d", &a[i][j]);
}
getfail();
scanf("%s", s);
m = strlen(s);
for (int i = 0; i < m; ++i) b[i] = s[i]-'0';
kmp();
}
int main() {
while (scanf("%d", &n) != EOF) work();
return 0;
}
hdu 5972 Regular Number 字符串Shift-And算法 + bitset的更多相关文章
- HDU 5972 Regular Number
Regular Number http://acm.hdu.edu.cn/showproblem.php?pid=5972 题意: 给定一个字符串,求多少子串满足,子串的第i位,只能是给定的数(小于等 ...
- HDU 5972 Regular Number(字符串shift - and算法)
题目链接 HDU5972 2016 ACM/ICPC 大连区域赛 B题 我们预处理出$b[i][j]$,$b[i][j] = 1$的意义是数字$i$可以放在第$j$位. 然后就开始这个匹配的过程. ...
- HDU 5972 Regular Number(ShiftAnd+读入优化)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5972 [题目大意] 给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串 ...
- Regular Number 字符串匹配算法 Shift_and
Using regular expression to define a numeric string is a very common thing. Generally, use the shape ...
- HDU - 1711 A - Number Sequence(kmp
HDU - 1711 A - Number Sequence Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...
- mean shift聚类算法的MATLAB程序
mean shift聚类算法的MATLAB程序 凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. mean shift 简介 mean shift, 写的 ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- 字符串相似度算法(编辑距离算法 Levenshtein Distance)(转)
在搞验证码识别的时候需要比较字符代码的相似度用到“编辑距离算法”,关于原理和C#实现做个记录. 据百度百科介绍: 编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个 ...
- hdu 2665 Kth number
划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...
随机推荐
- mysql 5.7初始化默认密码错误
下载了一个mysql 5.7.17的安装包后,安装后怎么都启动不了,好在mysql安装是成功了,没办法只有使用命令行重新初始化设置了 我的mysql安装根目录为:C:\Program Files\My ...
- 常用模块之 os,json,shelve,xml模块
os 即操作系统 在 os 中提供了很多关于文件,文件夹,路径处理的函数 这是我们学习的重点 os.path 是os模块下专门用于处理路径相关的 python是一门跨平台语言,由于每个平台路径规则不同 ...
- python常用内置算法用到的单词音频
http://boscdn.bpc.baidu.com/v1/developer/990a728b-ca96-4bd9-9124-5357d829bf70.mp3 百度广播开发平台生成
- Base64及其Python实现
1. 什么是Base64 Base64是一种基于64个可打印字符来表示二进制数据的表示方法 Base64是一种编码方式,提及编码方式,必然有其对应的字符集合.在Base64编码中,相互映射的两个集合是 ...
- GoF23种设计模式之结构型模式之适配器模式
一.概述 将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 二.适用性 1.你想使用一个已经存在的类,但是它的接口不符合 ...
- docker时区正常,但java获得的时间早了8小时解决方法
我解决容器时区的方法是挂载宿主机的/etc/localtime 到容器的/etc/localtime,这时输入date命令容器时区显示正常,但是跑在容器中的java项目取到的时间却早了8小时. 查阅相 ...
- 初学js之多组图片切换实例
需求是以上效果展示.话不多说,直接代码显示,不涉及代码优化.已实现功能为目的. 先看html部分: <body> <div class="dream" id=&q ...
- V4L2学习(四)VIVI分析
vivi 相对于后面要分析的 usb 摄像头驱动程序,它没有真正的硬件相关层的操作,也就是说抛开了复杂的 usb 层的相关知识,便于理解 V4L2 驱动框架,侧重于驱动和应用的交互. 前面我们提到,V ...
- 查找两个链表的第一个交叉结点(Python实现)
题目 给定两个单链表,查找这两个单链表的第一个交叉节点. 例如:链表list_a为:a1→a2→c1→c2→c3,链表list_b为:b1→b2→b3→c1→c2→c3.那么它们第一个交叉结点为c1. ...
- P3387 【模板】缩点
题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...