呃,从网上看的题解,然而其实有点地方还没搞懂,先放在这,以后再回来理解。

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

题目:2013 is the 60 anniversary of Nanjing University of Science and Technology, and today happens to be the anniversary date. On this happy festival, school authority hopes that the new students to be trained for the parade show. You should plan a better solution to arrange the students by choosing some queues from them preparing the parade show. (one student only in one queue or not be chosen)
  Every student has its own number, from 1 to n. (1<=n<=10^5), and they are standing from 1 to n in the increasing order the same with their number order. According to requirement of school authority, every queue is consisted of exactly m students. Because students who stand adjacent in training are assigned consecutive number, for better arrangement, you will choose in students with in consecutive numbers. When you choose these m students, you will rearrange their numbers from 1 to m, in the same order with their initial one. 
  If we divide our students’ heights into k (1<=k<=25) level, experience says that there will exist an best viewing module, represented by an array a[]. a[i] (1<=i<=m)stands for the student’s height with number i. In fact, inside a queue, for every number pair i, j (1<=i,j<=m), if the relative bigger or smaller or equal to relationship between the height of student number i and the height of student number j is the same with that between a[i] and a[j], then the queue is well designed. Given n students’ height array x[] (1<=x[i]<=k), and the best viewing module array a[], how many well designed queues can we make at most?

 
Input
Multiple cases, end with EOF.
First line, 3 integers, n (1<=n<=10^5) m (1<=m<=n) k(1<=k<=25),
Second line, n students’ height array x[] (1<=x[i]<=k,1<=i<=n);
Third line, m integers, best viewing module array a[] (1<=a[i]<=k,1<=i<=m);
 
Output
One integer, the maximal amount of well designed queues.
 
Sample Input
10 5 10
2 4 2 4 2 4 2 4 2 4
1 2 1 2 1
 
Sample Output
1
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define PI acos((double)-1)
#define E exp(double(1))
#define K 100000+9
int g1[][K],g2[][K];
int a[K],b[K],n,m,k;
int nt[K];
int check(int x,int y)
{
int c1,c2,c3;
c1=c2=c3=;
if(g2[b[x]][x-]!=g2[b[y]][y-]-g2[b[y]][y-x])return ;
for(int i=;i<b[x];i++)c1+=g2[i][x-];
for(int i=;i<b[y];i++)c2+=g2[i][y-];
for(int i=;i<b[y];i++)c3+=g2[i][y-x];
return c1==c2-c3;
} void kmp_next(void)
{
memset(nt,,sizeof(nt));
for(int i=,j=;i<=m;i++)
{
while(j && !check(j+,i))
j=nt[j];
if(check(j+,i))j++;
nt[i]=j;
}
}
bool ok(int x,int y)
{
int c1,c2,c3;
c1=c2=c3=;
if(g2[b[x]][x-]!=g1[a[y]][y-]-g1[a[y]][y-x])return ;
for(int i=;i<b[x];i++)c1+=g2[i][x-];
for(int i=;i<a[y];i++)c2+=g1[i][y-];
for(int i=;i<a[y];i++)c3+=g1[i][y-x];
return c1==c2-c3;
}
int kmp(void )
{
int ans=;
for(int i=,j=;i<=n;i++)
{
while(j&&!ok(j+,i))
j=nt[j];
if(ok(j+,i))
j++;
if(j==m)
ans++,j=;
}
return ans;
}
int main(void)
{
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(g1,,sizeof(g1));
memset(g2,,sizeof(g2));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
for(int j=;j<=k;j++)g1[j][i]=g1[j][i-];
g1[a[i]][i]++;
}
for(int i=;i<=m;i++)
{
scanf("%d",&b[i]);
for(int j=;j<=k;j++)g2[j][i]=g2[j][i-];
g2[b[i]][i]++;
}
kmp_next();
printf("%d\n",kmp());
} return ;
}

hdu4749 kmp应用的更多相关文章

  1. hdu4749 kmp改进

    这题说的是给了一个模板串 然后又给了一个串 需要找出类似的按个模板串 , 改相等的位置要相等 该大于的位置到大于 我们将模板串做好失配指针就ok了,然后匹配和原来的匹配不同,这个匹配需要的是相对匹配, ...

  2. HDU4749 Parade Show(KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给出两个数字串A.B.问A中有多少不相交的子串a能匹配B.匹配的意思是a中任意两个位置i和 ...

  3. HDU-4749 Parade Show KMP算法 | DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给两个串S和P,求S串中存在多少个与P串的大小关系一样的串. 因为数字的范围是1<= ...

  4. KMP算法求解

    // KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...

  5. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  6. KMP算法

    KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...

  7. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  8. [KMP]【学习笔记】

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 Descript ...

  9. KMP算法实现

    链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...

随机推荐

  1. 【转】MYSQL 存储过程定时操作数据库

    这个涉及2个步骤,第一个就是建立存储过程: create procedure clear_table() begin drop database XXX end 第二步就是让其定时运行: 查看even ...

  2. 无状态会话Bean、有状态会话Bean、CMP与BMP中,哪一种Bean不需要自己书写连接数据库的代码?

    无状态会话Bean.有状态会话Bean.CMP与BMP中,哪一种Bean不需要自己书写连接数据库的代码? A.无状态会话Bean B.有状态会话Bean C.CMP D.BMP 解答:C

  3. 请写出一个超链接,点击链接后可以向zhangsan@d-heaven.com发送电子邮件。

    请写出一个超链接,点击链接后可以向zhangsan@d-heaven.com发送电子邮件. <a href=”mailto: zhangsan@d-heaven.com”>发邮件</ ...

  4. canvas图片base64编码后,ajax提交到后台

    如题,直接上JS var imgData = canvas.toDataURL("image/png"); var data = imgData.substr(22); $.pos ...

  5. 【vijos】1768 顺序对的值(特殊的技巧)

    https://vijos.org/p/1768 之前不知道为什么,我yy了一个n^2的做法,但是没能写出来..sad 然后看了题解才发现这题好神.. 为什么一定要照着题意找两个点然后算呢?这就是问题 ...

  6. 【BZOJ】1827: [Usaco2010 Mar]gather 奶牛大集会(树形dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1827 仔细想想就好了,, 每个点维护两个值,一个是子树的费用,一个是除了子树和自己的费用.都可以用d ...

  7. SmartUI2.0后续声明

    感谢很多朋友关注,因为今年一直在另外一个公司做顾问,网络环境管制相当严格,所以一直没有更新博客. 同时也很抱歉,SmartUI 2.0一直都没有下文.在次声明一下,SmartUI一直都在做,只不过Sm ...

  8. Java将视频转为缩略图--ffmpeg

    Java生成视频缩略图 对于上传视频生成缩略图使用的是ffmpeg进行生成的. 自己在网上找了如何进行编译ffmpeg的方法 但是感觉太复杂了 就到csdn上找到了编译好的ffmpeg文件 体会到ff ...

  9. Entity Framework基础

    http://blog.csdn.net/hurtlingsnail/article/details/53113934

  10. Eclipse虚拟内存不足【Eclipse中虚拟内存设置】

    Eclipse最近在做J2EE项目中 发现老是出现虚拟内存不足的提示 前2天去加了根内存 问题同样存在 为了让我在写代码时 不在出现那讨厌的内存不足的提示 也为了 不让那破机器再卡住 今天找到了解决方 ...