hdu4749 kmp应用

本文提供了一道关于排列组合与数据结构的算法题解答,题目要求计算最多能形成多少个符合特定条件的队伍。通过预处理高度数组及运用 KMP 算法优化匹配过程,实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

题目地址: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[30][K],g2[30][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=0;
    if(g2[b[x]][x-1]!=g2[b[y]][y-1]-g2[b[y]][y-x])return 0;
    for(int i=1;i<b[x];i++)c1+=g2[i][x-1];
    for(int i=1;i<b[y];i++)c2+=g2[i][y-1];
    for(int i=1;i<b[y];i++)c3+=g2[i][y-x];
    return c1==c2-c3;
}

void kmp_next(void)
{
    memset(nt,0,sizeof(nt));
    for(int i=2,j=0;i<=m;i++)
    {
        while(j && !check(j+1,i))
            j=nt[j];
        if(check(j+1,i))j++;
        nt[i]=j;
    }
}
bool ok(int x,int y)
{
    int c1,c2,c3;
    c1=c2=c3=0;
    if(g2[b[x]][x-1]!=g1[a[y]][y-1]-g1[a[y]][y-x])return 0;
    for(int i=1;i<b[x];i++)c1+=g2[i][x-1];
    for(int i=1;i<a[y];i++)c2+=g1[i][y-1];
    for(int i=1;i<a[y];i++)c3+=g1[i][y-x];
    return c1==c2-c3;
}
int kmp(void )
{
    int ans=0;
    for(int i=1,j=0;i<=n;i++)
    {
        while(j&&!ok(j+1,i))
            j=nt[j];
        if(ok(j+1,i))
            j++;
        if(j==m)
            ans++,j=0;
    }
    return ans;
}
int main(void)
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        memset(g1,0,sizeof(g1));
        memset(g2,0,sizeof(g2));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            for(int j=1;j<=k;j++)g1[j][i]=g1[j][i-1];
            g1[a[i]][i]++;
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&b[i]);
            for(int j=1;j<=k;j++)g2[j][i]=g2[j][i-1];
            g2[b[i]][i]++;
        }
        kmp_next();
        printf("%d\n",kmp());
    }

    return 0;
}

 

转载于:https://www.cnblogs.com/weeping/p/5730978.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值