Sequence I
Sequence I (hdu 5918)
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1938 Accepted Submission(s): 730
Problem Description
Mr. Frog has two sequences a1,a2,⋯,an and b1,b2,⋯,bm and a number p. He wants to know the number of positions q such that sequence b1,b2,⋯,bmis exactly the sequence aq,aq+p,aq+2p,⋯,aq+(m−1)p where q+(m−1)p≤n and q≥1.
Input
The first line contains only one integer T≤100, which indicates the number of test cases.
Each test case contains three lines.
The first line contains three space-separated integers 1≤n≤106,1≤m≤106 and 1≤p≤106.
The second line contains n integers a1,a2,⋯,an(1≤ai≤109).
the third line contains m integers b1,b2,⋯,bm(1≤bi≤109).
Output
For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the number of valid q’s.
Sample Input
Sample Output
Case #1: 2
Case #2: 1
//题意: 字符串匹配,就是,n 长主串,m 长匹配串,k 长间隔,问有多少种匹配?
分成 k 组就好,这题可以用来测测你的 KMP 模板哦,数据还可以,就是,就算是朴素匹配也能过。。。
做完我算是真的理解KMP了,对于字符串,有个 \0 结尾的特性,所以优化的 next 是可行的,但是这种却不行,而且,优化的并不好求匹配数。
KMP 模板 : http://www.cnblogs.com/haoabcd2010/p/6722073.html
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <vector> 5 using namespace std; 6 #define MX 1000005 7 8 int n,m,p; 9 int ans; 10 int t[MX]; 11 vector<int> zu[MX]; 12 int net[MX]; 13 14 void Init() 15 { 16 for (int i=0;i<=n;i++) 17 zu[i].clear(); 18 } 19 20 void get_next() 21 { 22 int i=0,j=-1; 23 net[0]=-1; 24 while (i<m) 25 { 26 if (j==-1||t[i]==t[j]) net[++i]=++j; 27 else j = net[j]; 28 } 29 30 for (int i=0;i<=m;i++) 31 printf("%d ",net[i]); 32 printf("\n"); 33 } 34 35 void KMP(int x) 36 { 37 int i=0,j=0; 38 int len = zu[x].size(); 39 while(i<len&&j<m) 40 { 41 if (j==-1||zu[x][i]==t[j]) 42 { 43 i++,j++; 44 } 45 else j=net[j]; 46 if (j==m) 47 { 48 ans++; 49 j = net[j]; 50 } 51 } 52 } 53 54 int main() 55 { 56 int T; 57 scanf("%d",&T); 58 for(int cas=1;cas<=T;cas++) 59 { 60 scanf("%d%d%d",&n,&m,&p); 61 Init(); 62 for (int i=0;i<n;i++) 63 { 64 int x; 65 scanf("%d",&x); 66 zu[i%p].push_back(x); 67 } 68 for (int i=0;i<m;i++) 69 scanf("%d",&t[i]); 70 get_next(); 71 72 ans = 0; 73 for (int i=0;i<p;i++) KMP(i); 74 printf("Case #%d: %d\n",cas,ans); 75 } 76 return 0; 77 }
View Code
转载于:https://www.cnblogs.com/haoabcd2010/p/6842448.html
Sequence I相关推荐
- Bi-LSTM-CRF for Sequence Labeling
做了一段时间的Sequence Labeling的工作,发现在NER任务上面,很多论文都采用LSTM-CRFs的结构.CRF在最后一层应用进来可以考虑到概率最大的最优label路径,可以提高指标. 一 ...
- Oracle 12C -- 基于sequence的列的默认值
12C支持先创建一个sequence,然后再将该sequence指定为某个列的值的默认表达式. 和"identity column"具有以下不同点: ·对列的个数没有限制 ·seq ...
- DP UVALive 6506 Padovan Sequence
题目传送门 /*题意:两行数字,相邻列一上一下,或者隔一列两行都可以,从左到右选择数字使和最大DP:状态转移方程:dp[i][j] = max (dp[i][j], dp[1-i][j-1] + a[ ...
- HDOJ-2062 :Subset sequence(DP)
题目:求子集序列 Consider the aggregate An= { 1, 2, -, n }. For example, A1={1}, A3={1,2,3}. A subset sequen ...
- Gold Code,Gold Sequence
Gold Code Gold Code是以Robert Gold的名字命名的.它是一组特殊的二进制随机(伪随机)序列,其中成员序列之间的相关性很小.由于这种特性(较小的相关性),它被广泛地用作各种无线 ...
- HDU 1711 Number Sequence(KMP算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/ ...
- 【C++】C++11 STL算法(一):非修改序列操作(Non-modifying sequence operations)
目录 一.all_of.any_of.none_of: 1.官方说明 2.谓词 3.STL算法对谓词的说明 4.谓词的五种模式 5.all_of (C++ 11) 6.any_of (C++ 11) ...
- Sequence point 中文
摘自维基百科: In C[4] and C++,[5] sequence points occur in the following places. (In C++, overloaded opera ...
- swift Sequence 和 SubSequence
1 序列 Sequence 序列协议是集合类型结构中的基础. 一个序列是代表有一系列具有相同类型的值,并且对这些值进行迭代. 协议中主要有两个参数,一个是元素Element,一个就是迭代器Iterat ...
- 创新类编辑推荐:Sequence iBPMS平台
创新类编辑推荐是Sequence,一个"iBPMS"工作流管理平台.本文介绍了该产品以及用户是如何成功使用该平台的. SearchSOA的编辑会定期为创新性和市场影响而表彰应用集成 ...
最新文章
- 智能合约的48个应用场景介绍
- 案例开发分析 || ​​​​​​​Scheduler组件
- 揭秘阿里中台!一文看懂阿里推荐业务的两大利器
- protocol buffer使用小例
- 【猜画小歌】辅助插件FunnyDraw江湖召集令
- 什么是激光扫描测量仪
- GridMask:SOTA 数据增广方法,显著改进分类、检测、分割效果
- centos 搭建php运行环境
- Spring 事务使用详解
- python类型提示模块包_Python checktypes包_程序模块 - PyPI - Python中文网
- python中字符a如何变成b_python 如何把'a=b'这样的字符解析成dict类型
- 零基础学计算机编程教学视频教程,IT编程入门教程视频免费资源分享
- 小程序的好处是什么?
- 单片机编程入门基础知识(新手必看)
- C# C++ 互操作:C++向C#输出不定长数组或指针的实现
- 面试题:如果要画一只鸟和一个人,你会如何构图?
- 《深入理解Android 卷III》第七章 深入理解SystemUI(完整版)
- 文件监控——watchdog详解
- 被315点名的流氓下载器,又回来了…
- 创客机器人的课程简单易学吗