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

  题意:给两个串S和P,求S串中存在多少个与P串的大小关系一样的串。

  因为数字的范围是1<=k<=25之间,所以可以暴力的求25*25次KMP。当然完全没有必要这样做,在KMP的时候记录各个数的所表示的数就可以了,只需要求一遍KMP,复杂度降为O(25*n)。

 //STATUS:C++_AC_125MS_1596KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e60;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int next[N],s[N],p[N],w[],f[];
int T,n,m,k; void getnext(int *s,int len)
{
int j=,k=-;
next[]=-;
while(j<len){
if(k==- || s[k]==s[j])
next[++j]=++k;
else k=next[k];
}
} int solve()
{
int i,j,ret=,x,la=-;
for(i=j=;i<n;i++){
while(){
for(x=;x<=k;x++){
if(f[x]>=j){w[x]=-;continue;}
w[x]=p[i-j+f[x]];
}
if((j==-||w[s[j]]==-) || p[i]==w[s[j]])break;
j=next[j];
}
j++;
if(j==m && i>=la){la=i+m;ret++;}
}
return ret;
} int main(){
// freopen("in.txt","r",stdin);
int i,j,ans;
while(~scanf("%d%d%d",&n,&m,&k))
{
for(i=;i<n;i++){
scanf("%d",&p[i]);
}
mem(f,-);
for(i=;i<m;i++){
scanf("%d",&s[i]);
if(f[s[i]]==-)f[s[i]]=i;
}
p[n]=;s[m]=; getnext(s,m);
ans=solve(); printf("%d\n",ans);
}
return ;
}

HDU-4749 Parade Show KMP算法 | DP的更多相关文章

  1. hdu 1358:Period(KMP算法,next[]数组的使用)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. HDU 4749 Parade Show(贪心+kmp)

    题目链接 题目都看不懂,做毛线...看懂了之后就是kmp出,所有的匹配区间,然后DP可以写,贪心也可以做把,DP应该需要优化一下,直接贪,也应该对的,经典贪心问题. #include<iostr ...

  3. HDU 4749 Parade Show(暴力水果)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 Problem Description   2013 is the 60 anniversary ...

  4. HDU 4749 Parade Show 2013 ACM/ICPC Asia Regional Nanjing Online

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题目大意:给一个原序列N,再给出一个序列M,问从N中一共可以找出多少个长度为m的序列,序列中的数 ...

  5. hdu 1696 Oulipo(KMP算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题意 查询字符串 $p$ 在字符串 $s$ 中出现了多少次,可重叠. 题解 KMP模板题. Ti ...

  6. HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  7. HDU 4749: Parade Show

    看大神代码,发现上交大神很棒的一个思路 题意: 在源数字串中找出尽量多的连续子串,要求子串任意两值的大小关系与目标串相同位置的值的大小关系相同.求源串能拿出的子串的最大数量. 关键词: RK-Hash ...

  8. poj 2406:Power Strings(KMP算法,next[]数组的理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30069   Accepted: 12553 D ...

  9. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

随机推荐

  1. mapper device&lvm

    http://www.ibm.com/developerworks/cn/linux/l-devmapper/ http://baike.baidu.com/view/361916.htm?fr=al ...

  2. python - PipeMapRed.waitOutputThreads(): subprocess failed with code 1

    hadoop上执行mapreduce streaming python程序报错, 报错详细信息为 python - PipeMapRed.waitOutputThreads(): subprocess ...

  3. OpenRisc-52-run openrisc&orpmon on ml501 board

    引言 之前关于openrisc的内容,几乎都是基于opencores官方的ordb2a那个开发板的,但是,实际情况是有那个板子的人不是很多,所以目前我在做一个与之类似的板子,并且很快就会跟大家见面,这 ...

  4. 基于XMPP的即时通信系统的建立(一)— XMPP基础概念

    相关背景 IM(Instant Messaging)正在被广泛使用,特别是公司与它们的客户互动连接方案以及互联网与Web2.0相关的应用.为了解决即时通信的标准问题,IETF(互联网工程任务组 The ...

  5. 真正解决ASP.NET每一个页面首次访问超级慢的问题 (转载)

    原文:http://www.afuhao.com/article_articleId-219.shtml 摘要:ASP.NET页面首次打开很慢,但别的页面如果没有访问过,去访问也会慢.你也许认为它是在 ...

  6. bzoj4177: Mike的农场

    类似于最大权闭合图的思想. #include<cstdio> #include<cstring> #include<iostream> #include<al ...

  7. Android、iPhone和Java三个平台一致的加密工具

    先前一直在做安卓,最近要开发iPhone客户端,这其中遇到的最让人纠结的要属Java.Android和iPhone三个平台加解密不一致的问题. 因为手机端后台通常是用JAVA开发的Web Servic ...

  8. [转] Asp.net Report Viewer 简单实例

    原文链接:http://www.aspsnippets.com/Green/Articles/ASPNet-Report-Viewer-control-Tutorial-with-example.as ...

  9. HDU 3746 Cyclic Nacklace 环形项链(KMP,循环节)

    题意: 给一个字符串,问:要补多少个字符才能让其出现循环?出现循环是指循环节与字符串长度不相等.比如abc要补多个变成abcabc.若已经循环,输出0. 思路: 根据最小循环节的公式,当len%(le ...

  10. apache多站点配置+多端口配置

    Apache多站点配置 这种方法是配置不同的地址访问不同的文件夹的配置 1:修改本机的hosts文件,如下: 示例: 127.0.0.1 localhost 127.0.0.1 www.dede.co ...