题意

给你一个数字序列S,再给一个数字序列pattern,S和pattern中的数字都是1到s(s<=25)。每个序列里的数字都有个排名,也就是第几小,现在我们要用pattern来匹配S。在本题中匹配是指每个数字的排名都一样,即同是第k小,最后输出匹配的所有位置。

思路

KMP好题,对KMP的理解又透彻了一点点~

我们考虑两个字符串A,B

在此题中,A[1],A[2],…,A[k]与B[1],B[2],…,B[k]匹配条件:

若A[1],A[2],…,A[k-1]与B[1],B[2],…,B[k-1]匹配,则加上A[k]与B[k]仍然匹配的条件是:

必须在与k无关的时间复杂度内完成该操作

然而,由于A[1],A[2],…,A[k-1]与B[1],B[2],…,B[k-1]已经匹配,可以简化比较操作
考虑到字符集很小(不超过25),我们可以定义以下几个变量:

Occur[p],Low[i],High[i]可能不存在,若存在则取最小的符合条件的x

则A[1],A[2],…,A[k-1]与B[1],B[2],…,B[k-1]匹配,加上A[k]与B[k]仍然匹配的条件可简化为:

接下来就可以用O(SK)时间内预处理求出Occur[p],Low[i],High[i],然后套用kmp算法,总时间复杂度为O(N+SK)

代码

[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;

typedef long long LL;
int n, k, s;
int S[100005], pattern[25005];
int low[25005], high[25005], ranks_pos[30], next[100005];
vector <int> ans;

void init(){
MEM(ranks_pos, -1); MEM(low, -1); MEM(high, -1);
for (int i = 0; i < k; ++ i){
int j = pattern[i] - 1;
while(j){
if (~ranks_pos[j]){ low[i] = j; break; }
j --;
}
j = pattern[i] + 1;
while(j < s+1){
if (~ranks_pos[j]){ high[i] = j; break; }
j ++;
}
if (ranks_pos[pattern[i]] == -1)
ranks_pos[pattern[i]] = i;
}
}
bool check(int A[], int B[], int k, int pos){
if (~ranks_pos[A[pos]]){
if (B[k+ranks_pos[A[pos]]] != B[k+pos]) return false;
}
if (~low[pos] && ~ranks_pos[low[pos]]){
if (B[k+ranks_pos[low[pos]]] >= B[k+pos]) return false;
}
if (~high[pos] && ~ranks_pos[high[pos]]){
if (B[k+ranks_pos[high[pos]]] <= B[k+pos]) return false;
}
return true;
}
void get_next(){
int j = -1;
next[0] = -1;
for (int i = 1; i < k; ++ i){
while(j > -1 && !check(pattern, pattern, i-j-1, j+1)) j = next[j];
if (check(pattern, pattern, i-j-1, j+1)) j ++;
next[i] = j;
//printf("next%d = %d\n", i, next[i]);
}
}
void kmp(){
ans.clear();
get_next();
int j = -1;
for (int i = 0; i < n; ++ i){
while(j > -1 && !check(pattern, S, i-j-1, j+1)) j = next[j];
if (check(pattern, S, i-j-1, j+1)) j ++;
if (j == k - 1){
ans.push_back(i - k + 1);
j = next[j];
}
}
}
int main(){
while(scanf("%d %d %d", &n, &k, &s) != EOF){
for (int i = 0; i < n; i ++) scanf("%d", &S[i]);
for (int i = 0; i < k; i ++) scanf("%d", &pattern[i]);
init();
kmp();
printf("%d\n", ans.size());
for (int i = 0; i < (int)ans.size(); ++ i){
printf("%d\n", ans[i]+1);
}
}
return 0;
}
[/cpp]

POJ 3167 Cow Pattern ★(KMP好题)的更多相关文章

  1. POJ 3167 Cow Patterns (KMP+前缀和)

    题意:给你两串数字,长度分别为n和m,数字大小在[1,25].当后一串数字每个数字的排名位置与前一串数字(任一长度为m的子串)每个数字的排名位置一致时就完全匹配,最后求哪些位置是完全匹配的. 例如:1 ...

  2. POJ 3167 Cow Patterns(模式串浮动匹配)

    题目链接:http://poj.org/problem?id=3167 题意:模式串可以浮动的模式匹配问题给出模式串的相对大小,需要找出模式串匹配次数和位置. 思路:统计比当前数小,和于当前数相等的, ...

  3. POJ:3461-Oulipo(KMP模板题)

    原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...

  4. POJ 3176 Cow Bowling (水题DP)

    题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...

  5. poj 3461 Oulipo(KMP模板题)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36903   Accepted: 14898 Descript ...

  6. POJ 3461 Oulipo(KMP裸题)

    Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...

  7. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  8. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  9. POJ 3045 Cow Acrobats (贪心)

    POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...

随机推荐

  1. sql 服务器链接远程 sql 服务器 脚本

    exec sp_droplinkedsrvlogin 'test',null exec sp_dropserver 'test' exec sp_addlinkedserver@server='Tes ...

  2. AtCoder Regular Contest 080 D - Grid Coloring

    地址:http://arc080.contest.atcoder.jp/tasks/arc080_b 题目: D - Grid Coloring Time limit : 2sec / Memory ...

  3. Uninstalling JIRA applications from Linux

     If you wish to re-install JIRA in 'unattended mode', do not uninstall your previous installation of ...

  4. Pentester中的XSS详解

    本次做的是Web For Pentester靶机里面的XSS题目,一共有9道题目. 关于靶机搭建参考这篇文章:渗透测试靶机的搭建 第1题(无过滤措施) 首先在后面输入xss: http://10.21 ...

  5. STC12C系列单片机PWM脉宽调制

    最近给别人做了一个小东西,MCU选的是STC12C5A60S2 ,需要用到PWM控制功能. 在网上找了一下,发现解释的不尽人意,无奈之下自己琢磨数据手册弄明白了. 首先,STC12C5A60S2内置有 ...

  6. Java:出现错误提示(java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date)

    Java:出现错误提示(java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date) 原因分析: ...

  7. 如何在Kubernetes集群动态使用 NAS 持久卷

    1. 介绍: 本文介绍的动态生成NAS存储卷的方案:在一个已有文件系统上,自动生成一个目录,这个目录定义为目标存储卷: 镜像地址:registry.cn-hangzhou.aliyuncs.com/a ...

  8. 20145310 GDB调试汇编堆栈分析

    GDB调试汇编堆栈分析 由于老师说要逐条分析汇编代码,所以我学习卢肖明同学的方法,重新写了一篇博客. 代码: #include<stdio.h> short addend1 = 1; st ...

  9. 20145321 《Java程序设计》第5周学习总结

    20145321 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 1.Try.catch:Java中所有错误都会被打包为对象,通过try和catch语法可以对代表错误的对象做 ...

  10. spring MVC Action里面怎么设置UTF-8编码集

    /* 编码转换,确保写数据库的时候不会出现乱码 */ public class CodingConvert{ public CodingConvert(){ // } public String to ...