【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5972

【题目大意】

  给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串的长度为n,并且第i个字符需要在给定的字符集合Si中

【题解】

  利用ShiftAnd匹配算法。

  bt[i]表示字符i允许在哪些位置上出现,我们将匹配成功的位置保存在dp中,那么就可以用dp[i]=dp[i-1]<<1&bt[s[i]]来更新答案了

  利用滚动数组和bitset可以来优化这样的运算,当一个位置的匹配在更新的过程中没有丢失,

  即始终在特定模式中直到定长,那么这个位置就是成功匹配位,复杂度为O(nm/64)

  由于输入的数据量庞大,因此需要读入和输出优化。

  终于AC了,补上大连赛区的遗憾。

【代码】

#include <cstdio>
#include <bitset>
#include <cstring>
using namespace std;
const int M=1010,N=5000010,U=256;
bitset<M> dp[2],bt[U];
int n,m,x,id[U],cnt,l;
char s[N];
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;
};
void init(){
cnt=0;
for(int i='0';i<='9';i++)id[i]=cnt++;
}
void ShiftAnd(int n,int m){
int cur=1,f=0;
dp[0].reset(); dp[0].set(0);
for(int i=1;i<=n;i++,cur^=1){
dp[cur]=dp[cur^1]<<1&bt[id[s[i]]];
dp[cur].set(0);
if(dp[cur][m]){
for(int j=i-m+1;j<=i;j++)fastIO::print(s[j]);
fastIO::println();
}
}
}
int main(){
//freopen("demo.in","r",stdin);
//freopen("demo.out","w",stdout);
init();
while(fastIO::RI(m)){
for(int i=0;i<cnt;i++)bt[i].reset();
for(int i=1;i<=m;i++){
fastIO::RI(l);
for(int j=1;j<=l;j++){
fastIO::RI(x);
bt[x].set(i);
}
}fastIO::read(s+1);
n=strlen(s+1);
ShiftAnd(n,m);
}return 0;
}

  

HDU 5972 Regular Number(ShiftAnd+读入优化)的更多相关文章

  1. HDU 5972 Regular Number

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

  2. hdu 5972 Regular Number 字符串Shift-And算法 + bitset

    题目链接 题意 给定两个串\(S,T\),找出\(S\)中所有与\(T\)匹配的子串. 这里,\(T\)的每位上可以有若干(\(\leq 10\))种选择,匹配的含义是:对于\(S\)的子串的每一位, ...

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

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

  4. hdu 5898 odd-even number 数位DP

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

  5. hdu 2665 Kth number

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

  6. fread读入优化,寻找速度极限

    序: 在之前的测试中,我们比较了四种读入方式,发现使用读入优化是最快的选择,但是我们知道fread()是比它更快的方法.这一次,我们对比四种读入优化,探寻C++读取速度的极限. 分别是getchar( ...

  7. OI黑科技:读入优化

    利用getchar()函数加速读入. Q:读入优化是什么? A :更加快速地读入一些较大的数字. Q:scanf不是已经够快了吗? A:Naive,scanf还是不!够!快! Q:那怎么办呢? A:我 ...

  8. hdu 4670 Cube number on a tree(点分治)

    Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  9. ACM:读入优化

    两个简单的读入优化 int getin(){ ;; while(!isdigit(tmp=getchar()) && tmp!='-'); ,tmp=getchar(); )+(ans ...

随机推荐

  1. Struts2中在Action里面向前端页面传值的方法总结

    由于在Action中并不能直接诶访问Servlet API,但它提供了相关类ActionContext来访问HttpServletRequest.HttpSession和ServletContext, ...

  2. 05-0. 求序列前N项和(15)

    本题要求编写程序,计算序列 2/1+3/2+5/3+8/5+... 的前N项之和.注意该序列从第2项起,每一项的分子是前一项分子与分母的和,分母是前一项的分子. 输入格式: 输入在一行中给出一个正整数 ...

  3. 计算机专业-世界大学学术排名,QS排名,U.S.NEWS排名

    2015年美国大学计算机专业排名 计算机专业介绍:计算机涉及的领域非常广泛,其分支学科也是非常多.所以在美国将主要的专业方向分为人工智能,程序应用,计算机系统(Systems)以及计算机理论(theo ...

  4. API之IP地址查询---权威的IP地址查询接口集合

    原文地址:http://yushine.iteye.com/blog/1717586 推荐实用IP138 http://www.baidu.com/s?wd=IP&rsv_spt=1& ...

  5. ubuntu qt X11开发环境

  6. Android UI ActionBar功能-自动隐藏 Action Bar

    为了使ActionBar不影响Activity的布局内容,我们还可以设置ActionBar,将其设置为透明,并且让Activity是头部自动空出一个ActionBar的空间: 官方文档:http:// ...

  7. 网易云课堂_程序设计入门-C语言_期末考试编程题

    1 字数统计(10分) 题目内容: 你的程序要读入一篇英文文章,然后统计其中的单词数来输出.需要统计的数据为: 总的单词数量: 含有1个字母到10个字母的单词的数量. 单词和单词的间隔是由以下标点符号 ...

  8. P酱的冒险旅途(思维)

    P酱的冒险旅途 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  9. java web从零单排第十七期《struts2》数据标签库(1)

    1.s:action标签: 此标签的作用是在JSP页面中访问action类的数据,执行某些操作,并返回相应的数据.其属性及意义如下: 属性名 是否必需 默认值 类型 说明介绍 executeResul ...

  10. 一个tabBarController管理多个Storyboard

    随着项目的业务逻辑越来越复杂,随着项目越来越大,那么我们Storybard中得控制器就越来越多, 就越来越难以维护.然而使用Storyborad又能更方便的帮助我们做屏幕适配(PS:尤其在6.6+出来 ...