hdu4749 kmp应用
呃,从网上看的题解,然而其实有点地方还没搞懂,先放在这,以后再回来理解。
题目地址: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?
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);
2 4 2 4 2 4 2 4 2 4
1 2 1 2 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应用的更多相关文章
- hdu4749 kmp改进
这题说的是给了一个模板串 然后又给了一个串 需要找出类似的按个模板串 , 改相等的位置要相等 该大于的位置到大于 我们将模板串做好失配指针就ok了,然后匹配和原来的匹配不同,这个匹配需要的是相对匹配, ...
- HDU4749 Parade Show(KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给出两个数字串A.B.问A中有多少不相交的子串a能匹配B.匹配的意思是a中任意两个位置i和 ...
- HDU-4749 Parade Show KMP算法 | DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给两个串S和P,求S串中存在多少个与P串的大小关系一样的串. 因为数字的范围是1<= ...
- KMP算法求解
// KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...
- 简单有效的kmp算法
以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...
- KMP算法
KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [KMP]【学习笔记】
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36916 Accepted: 14904 Descript ...
- KMP算法实现
链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...
随机推荐
- plsql 环境搭建(sqlplus环境设置)
1. 初始化参数脚本 修改glogin.sql 脚本, 此脚本为登录 sql/plus时会读取的脚本, 可以在这个脚本上设置一些参数, 比如 set serveroutput on 等等, 那么在登录 ...
- 阐述二维码的原理以及使用google api和PHP QR Code来生成二维码
一.什么是二维码:二维码 (2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的.在许多种类的二维条码中,常用的码 ...
- 数据挖据之GeoHash核心原理解析
引子 机机是个好动又好学的孩子,平日里就喜欢拿着手机地图点点按按来查询一些好玩的东西.某一天机机到北海公园游玩,肚肚饿了,于是乎打开手机地图,搜索北海公园附近的餐馆,并选了其中一家用餐. 饭饱之后机机 ...
- Android基站定位
Android基站定位 一.通过手机信号获取基站信息 通过TelephonyManager 获取lac:mcc:mnc:cell-id(基站信息)的解释: MCC.Mobile Country C ...
- 用MathType编辑上下尖括号有什么技巧
在MathType中,同一个数学符号可以进行各种变换方向的使用,就比如箭头符号,任意方向都可以使用,这也是很常见的.数学中的符号能够根据各种特殊需要进行灵活使用,除了箭头符号之外,其它符号也可以,比如 ...
- 简单的php基于curl的反向代理程序
起因: 经理:需要实现一个反向代理? 我: 简单,nginx分分钟配置好. 经理:嗯?没有nginx? 我: nodejs也行啊,网上有例子分分钟搞定. 经理:嗯?只有虚拟主机,只能上传php程序? ...
- 根据百度地图API得到坐标和地址并在地图上显示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- java jdk 1.6 下载
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-41940 ...
- 【BZOJ4504】K个串 可持久化线段树+堆
[BZOJ4504]K个串 Description 兔子们在玩k个串的游戏.首先,它们拿出了一个长度为n的数字序列,选出其中的一个连续子串,然后统计其子串中所有数字之和(注意这里重复出现的数字只被统计 ...
- quartz定时任务配置
参考:http://www.cnblogs.com/kay/archive/2007/11/02/947372.html Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quar ...