题目链接

题意

给定两个串\(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. ospf多区域实例配置

    需求:是pc1,pc2,pc3直接可以相互通信,ip分别pc1:192.168.1.2 pc2:192.168.3.2 pc3:192.168.5.2 LSW1配置: 首先划分vlan,vlan中配置 ...

  2. Python 正则表达式 贪心匹配和非贪心匹配

    Python的正则表达式默认是“贪心匹配”,即在有第二义的情况下,尽可能匹配最长的字符串,在正则表达式的花括号后面跟上问号,可以变为非贪心模式 >>> >>> ha ...

  3. ubuntu 压缩 解压 命令大全

    ubuntu下文件压缩/解压缩命令总结 http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz ...

  4. Python学习笔记(二):数据类型

    一.python中的数据类型 python中的数据类型包括:整型.浮点型.布尔型.字符串类型 整型(int)和浮点型(float) Python中的整型只有int,没有short.long:浮点型fl ...

  5. python3调取百度地图API输出某地点的经纬度信息

    1. 查看API接口说明 地址:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding 注:callback ...

  6. EF上下文对象创建之线程内唯一

    在一次请求中,即一个线程内,若是用到EF数据上下文对象,就创建一个,那么会造成数据混乱,每次创建的对象执行相应的数据库操作,此同时,其他的EF对象内获得的数据可能已经是“过期”的了.即这个数据已经变动 ...

  7. 《鸟哥的Linux私房菜》学习笔记(0)——磁盘与文件系统管理

    一.Linux的登陆流程 login: 用户名:每个用户名都有一个用户ID(用户标识符),计算机处理的就是用户ID(数字)而不是用户名(字符),. 认证机制:Authentication,比如密码或者 ...

  8. 极简配置phpstorm+xdebug进行断点调试

    以前调试的时候各种var_dump()就能得到结果,现在入手别人开发的工作,由于不了解业务和代码逻辑,又要去修改bug,就造成了修改bug效率低,所以又拾起来了xdbug,顺便总结了一下phpstor ...

  9. python 学习分享-实战篇选课系统

    # 角色:学校.学员.课程.讲师 # 要求: # 1. 创建北京.上海 2 所学校 # 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 # ...

  10. 数据结构之c++感悟

    #include<iostream.h> 头文件: #include<iostream.h> #define MAX 1024 typedef int Elemtype; ty ...