之前自己在做题的时候在网上找别人的题解,虽然当时理解,但时间一久就忘了。所以开个这个东西来记录自己的学习进程,方便自己的回顾,以及给他人提供题解。
开始冲击明年高二的省选!不再颓废!
好了,下面进入正题
Cow Patterns
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3144   Accepted: 1164

Description

A particular subgroup of K (1 <= K <= 25,000) of Farmer John's cows likes to make trouble. When placed in a line, these troublemakers stand together in a particular order. In order to locate these troublemakers, FJ has lined up his N (1 <= N <= 100,000) cows. The cows will file past FJ into the barn, staying in order. FJ needs your help to locate suspicious blocks of K cows within this line that might potentially be the troublemaking cows.

FJ distinguishes his cows by the number of spots 1..S on each cow's coat (1 <= S <= 25). While not a perfect method, it serves his purposes. FJ does not remember the exact number of spots on each cow in the subgroup of troublemakers. He can, however, remember which cows in the group have the same number of spots, and which of any pair of cows has more spots (if the spot counts differ). He describes such a pattern with a sequence of K ranks in the range 1..S. For example, consider this sequence:

      1 4 4 3 2 1

In this example, FJ is seeking a consecutive sequence of 6 cows from among his N cows in a line. Cows #1 and #6 in this sequence have the same number of spots (although this number is not necessarily 1) and they have the smallest number of spots of cows #1..#6 (since they are labeled as '1'). Cow #5 has the second-smallest number of spots, different from all the other cows #1..#6. Cows #2 and #3 have the same number of spots, and this number is the largest of all cows #1..#6.

If the true count of spots for some sequence of cows is:

 5 6 2 10 10 7 3 2 9

then only the subsequence 2 10 10 7 3 2 matches FJ's pattern above.

Please help FJ locate all the length-K subsequences in his line of cows that match his specified pattern.

Input

Line 1: Three space-separated integers: N, K, and S

Lines 2..N+1: Line i+1 describes the number of spots on cow i.

Lines N+2..N+K+1: Line i+N+1 describes pattern-rank slot i.

Output

Line 1: The number of indices, B, at which the pattern matches

Lines 2..B+1: An index (in the range 1..N) of the starting location where the pattern matches.

Sample Input

9 6 10
5
6
2
10
10
7
3
2
9
1
4
4
3
2
1

Sample Output

1
3

Hint

Explanation of the sample:

The sample input corresponds to the example given in the problem statement.

There is only one match, at position 3 within FJ's sequence of N cows.

题目大意:

稍稍神奇的KMP。

模式串和匹配串匹配的条件是:匹配串各个位置数字的大小关系与模式串相同。或者说:把匹配串离散化后与模式串相同。

思路:

在普通的KMP中,当时s[i..j]和t[k..l]已经匹配时,s[i..j+1]和t[k..l+1]匹配的条件是s[j+1]=t[l+1];而在本题中,要求匹配串各个位置数字的大小关系与模式串相同,

那么s[i..j+1]和t[k..l+1]匹配的条件便是s[i..j+1]中小于(等于)s[j+1]的数的个数与t[k..l]中小于(等于)t[l+1]的数的个数相同。用一个树状数组来记录s[i..j]/t[k..l]中小于(等于)

s[j+1]/t[l+1]的数的个数即可

其他与普通KMP基本相同。

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>using namespace std;int m1[100011],m2[100011],next[100011];
int a[100011],b[100011],ans[100011],f[101];
int n,m,t,i,j,xzq,x;int lowbit(int x)
{return x&-x;
}int get(int k)
{int l;l=0;while(k>0){l+=f[k];k-=lowbit(k);}return l;
}void add(int k,int v)
{while(k<=t){f[k]+=v;k+=lowbit(k);}
}int main()
{scanf("%d%d%d",&m,&n,&t);for(i=1;i<=m;i++)scanf("%d",&b[i]);for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=n;i++){m1[i]=get(a[i]-1);m2[i]=get(a[i])-m1[i];add(a[i],1);}memset(f,0,sizeof(f));x=0;for(i=2;i<=n;i++){while(x!=0&&(get(a[i]-1)!=m1[x+1]||get(a[i])-get(a[i]-1)!=m2[x+1])){for(j=i-x;j<=i-1-next[x];j++)add(a[j],-1);x=next[x];}if(get(a[i]-1)==m1[x+1]&&get(a[i])-get(a[i]-1)==m2[x+1]){add(a[i],1);x++;}next[i]=x;}x=0;memset(f,0,sizeof(f));for(i=1;i<=m;i++){while(x!=0&&(get(b[i]-1)!=m1[x+1]||get(b[i])-get(b[i]-1)!=m2[x+1])){for(j=i-x;j<=i-1-next[x];j++)add(b[j],-1);x=next[x];}if(get(b[i]-1)==m1[x+1]&&get(b[i])-get(b[i]-1)==m2[x+1]){add(b[i],1);x++;}if(x==n){ans[++xzq]=i-n+1;for(j=i-x+1;j<=i-next[x];j++)add(b[j],-1);x=next[x];}}printf("%d\n",xzq);for(i=1;i<=xzq;i++)printf("%d\n",ans[i]);
}

转载于:https://www.cnblogs.com/applejxt/p/3801458.html

poj 3167(KMP+树状数组)相关推荐

  1. POJ 2481 Cows POJ 2352 Stars(树状数组妙用)

    题目链接:POJ 2481 Cows POJ 2352 Stars 发现这两个题目都跟求逆序数有着异曲同工之妙,通过向树状数组中插入点的位置,赋值为1,或者++,然后通过求和来判断比当前 点 &quo ...

  2. POJ 2299 Ultra-QuickSort(树状数组 + 离散)

    链接:http://poj.org/problem?id=2299 题意:给出N个数组成的数列A(0 <= A[i] <= 999,999,999),求该数列逆序对的数量. 分析:题目所谓 ...

  3. hdu 4125 Moles(kmp+树状数组)

    题目链接:hdu 4125 Moles 题意: 给你n个数,让你按键值建一个平衡二叉树,然后奇数为0,偶数为1,然后可以遍历这颗树得到一个欧拉序列,现在给你一个串,问你出现了几次. 题解: 建树的时候 ...

  4. POJ 2299 Ultra-QuickSort(树状数组+离散化)

    题目大意: 就是说,给你一个序列,然后让你求出这个序列有多少个逆序对,所谓逆序对就是对于这个序列中的元素有a[i]>a[j] 且i<j存在. 其实原题是这样说的,给你一个序列,让你用最少的 ...

  5. 2017 ACM-ICPC乌鲁木齐网络赛 G. Query on a string(KMP+树状数组)

    题目链接:https://www.jisuanke.com/contest/870 题意: 给出两个字符串S和T,Q次操作:①C a b表示将第a个字符改为b:②Q l r表示T在S的子串[l, r] ...

  6. POJ 1990 (树状数组入门)

    MooFest Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a socia ...

  7. poj 2352 Stars 线段树(先建后查/边建边查)/树状数组三种方法思路详解,带你深入了解线段树难度⭐⭐⭐★

    poj 2352 Stars 目录 poj 2352 Stars 1.树状数组 2.线段树,先建树后查找 3.线段树,边建树边查找 Description Astronomers often exam ...

  8. poj 3321 Apple Tree(dfs序+树状数组求和模型)

    题目链接:http://poj.org/problem?id=3321 解题思路: 先dfs求出序列,将子树转化到dfs序列的区间内,接下来就是简单的树状数组求和模型了.水题. #include< ...

  9. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

最新文章

  1. 如何应対syn flood
  2. 在Project 2010中添加自定义任务窗格
  3. Android NDK学习笔记3:JNI访问Java属性、方法
  4. mysql死锁释放时间参数_由FTWRL导致的MySQL从库死锁分析及参数深究
  5. 关于用Java写的贪吃蛇游戏的一些感想
  6. virtualbox centos安装增强工具
  7. setsebool命令和设置命令
  8. MySQL8.0.19 JDBC下载与使用
  9. 用计算机分析卫星云图 进行实时天气,卫星云图,气象卫星云图,卫星云图高清实时滚动播放 - 围观天气...
  10. AndroidStudio制作登录和注册功能的实现,界面的布局介绍
  11. 介绍一款rar文件密码破解利器——RAR Password Unlocker
  12. 计算机图形学中点画线法
  13. python怎么变成动图_python 生成动图
  14. python中if not是什么意思_python中if not x: 和 if x is not None: 和 if not x is None的使用和区别...
  15. android 文件扫描MediaStore
  16. 多个PDF文件合并方法
  17. idea中push代码失败问题解决
  18. Python图像识别及操作
  19. C/C++描述 LeetCode周赛 5473. 灯泡开关 IV
  20. 数字孪生医院的智能化运营平台建设内容

热门文章

  1. python sql查询返回记录_干货!Python与MySQL数据库的交互实战
  2. 思科模拟器叫什么_扫盲!通过型号快速识别思科路由器,交换机,服务器等设备...
  3. java 多线程 临界区_【Java并发性和多线程】竞态条件与临界区
  4. linux man 手册翻译,close (linux man) 翻译
  5. python 列表操作详解,Python列表解析操作实例总结
  6. javascript 时间类型 Date
  7. Node.js webpack
  8. pytorch torch.rand
  9. javascript Event监听
  10. neo4j limit