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 ...
随机推荐
- 详解 JS 中 new 调用函数原理
JavaScript 中经常使用构造函数创建对象(通过 new 操作符调用一个函数),那在使用 new 调用一个函数的时候到底发生了什么?先看几个例子,再解释背后发生了什么. 1)看三个例子 1.1 ...
- cmf5分页相关
//分页配置在app/config.php 'paginate' => [ 'type' => '\cmf\paginator\Bootstrap', 'var_page' => ' ...
- tcl之变量-简单变量
- 12.2 VUE学习之-if判断,实践加减input里的值
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- A1035 Password (20)(20 分)
A1035 Password (20)(20 分) To prepare for PAT, the judge sometimes has to generate random passwords f ...
- 水题:HDU-1088-Write a simple HTML Browser(模拟题)
解题心得: 1.仔细读题,细心细心...... 2.题的几个要求:超过八十个字符换一行,<br>换行,<hr>打印一个分割线,最后打印一个新的空行.主要是输出要求比较多. 3. ...
- 12、python中的函数(高阶函数)
一.高阶函数 函数实际上也是一个对象,所以也能由变量指向一个函数对象,实际上函数名就是一个变量名.那么函数是传入变量作为参数的,如果传入的变量指向的是函数对象,这种函数就叫高阶函数. 高阶函数就是传入 ...
- 第 8 章: 模块, 包与分发---Word
第八章: 模块, 包 与 分发 描述: 大型Python程序以模块和包的形式组织.另外,Python标准库中包含大量模块.本章详细介绍模块和包系统.还将提供有关如何安装第三方模块和分发源代码的信息. ...
- 笔记-python-tutorial-4.controlflow( and function)
笔记-python-tutorial-4.controlflow( and function) 1. 函数 1.1. 定义函数 def name(x): “””函数的第一行语句可以是可 ...
- #include "*.c"文件的妙用
在看uCOS II V2.91版本源代码时,在ucos_ii.c源文件中发现下面的代码: #include <os_core.c> #include <os_flag.c> # ...