题目链接

题意

给定两个串\(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的更多相关文章

  1. HDU 5972 Regular Number

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

  2. HDU 5972 Regular Number(字符串shift - and算法)

    题目链接  HDU5972 2016 ACM/ICPC 大连区域赛 B题 我们预处理出$b[i][j]$,$b[i][j] = 1$的意义是数字$i$可以放在第$j$位. 然后就开始这个匹配的过程. ...

  3. HDU 5972 Regular Number(ShiftAnd+读入优化)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5972 [题目大意] 给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串 ...

  4. Regular Number 字符串匹配算法 Shift_and

    Using regular expression to define a numeric string is a very common thing. Generally, use the shape ...

  5. 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 ...

  6. mean shift聚类算法的MATLAB程序

    mean shift聚类算法的MATLAB程序 凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. mean shift 简介 mean shift, 写的 ...

  7. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  8. 字符串相似度算法(编辑距离算法 Levenshtein Distance)(转)

    在搞验证码识别的时候需要比较字符代码的相似度用到“编辑距离算法”,关于原理和C#实现做个记录. 据百度百科介绍: 编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个 ...

  9. hdu 2665 Kth number

    划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...

随机推荐

  1. 火狐IE event和target的兼容

    一.event对象 IE 中可以直接使用 window.event 对象,而 FF 中则不可以,解决方法之一如下: var theEvent = window.event || arguments.c ...

  2. ThinkPHP函数I代码优化

    ThinkPHP/Common/common.php 文件 I函数,主要用来获取一些gpc请求的变量的,函数有一部分代码是过滤变量的,每次都运行一次,其实是没有必要的. 如果你每次都像这样的方式调用的 ...

  3. ThinkPHP邮件发送S(Smtp + Mail + phpmailer)

    三种邮件发送介绍:(Smtp,Mail以及phpmailer)ThinkPhp 框架下开发. 邮件发送配置先前准备(用该账号做测试用):(这里用新浪邮箱服务器)将自己的新浪邮箱开通 POP3/SMTP ...

  4. 26.VUE学习之--提交表单不刷新页面,事件修饰符之使用$event与prevent修复符操作表单

    提交表单不刷新页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  5. Python_day01_作业笔记

    内容大纲: 1. python的出生与应用以及历史, python2x: 源码冗余,源码重复,源码不规范. python3x: 源码清晰优美简单.   2. python的种类. Cpython: 官 ...

  6. 免费证书Let’s Encrypt

    我们自己也可以签发 SSL 安全证书,但是我们自己签发的安全证书不会被主流的浏览器信任,所以我们需要被信任的证书授权中心( CA )签发的安全证书.而一般的 SSL 安全证书签发服务都比较贵,比如 G ...

  7. notification 使用的基本方法

    当某个应用程序希望向用户发出一些提示信息,而应用程序又不在前台,可以借助Notification来实现.发出一条通知后,手机最上方额通知栏会显示一个图标,下来状态栏以后可以看到详细内容. 一.通知的基 ...

  8. CQRS之旅——旅程5(准备发布V1版本)

    旅程5:准备发布V1版本 添加功能和重构,为V1版本发布做准备. "大多数人在完成一件事之后,就像留声机的唱片一样,一遍又一遍地使用它,直到它破碎,忘记了过去是用来创造更多未来的东西.&qu ...

  9. 洛谷P1540 机器翻译

    题目链接:https://www.luogu.org/problemnew/show/P1540

  10. 9.Iptables与Firewalld防火墙

    第9章 Iptables与Firewalld防火墙 章节简述: 保障数据的安全性是继保障数据的可用性之后最为重要的一项工作.防火墙作为公网与内网之间的保护屏障,在保障数据的安全性方面起着至关重要的作用 ...