【37.68%】【hdu 5918】Sequence I
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 706 Accepted Submission(s): 266
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,⋯,bm is 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
2
6 3 1
1 2 3 1 2 3
1 2 3
6 3 2
1 3 2 2 3 1
1 2 3
Sample Output
Case #1: 2
Case #2: 1
Source
2016中国大学生程序设计竞赛(长春)-重现赛
【题解】
意思是让你在
a1,a1+p,a1+2p,a1+3p…
a2,a2+p,a2+2p,a2 + 3p..
a3,a3+p,a3+2p,a3 +3p
…
ap,ap+p,ap+2p,ap+3p..
(a右边的东西都是下标;)
这p个序列里面找b数组的匹配数目;
用vector类处理出这个p个数列就好。
剩下的用KMP算法解决。
找完一个匹配之后,j==m。
这个时候让j= f[j];
就能继续找匹配了。
记住就好。不然每次都想好烦。
#include <cstdio>
#include <iostream>
#include <vector>const int MAXN = 2e6;
const int MAXM = 2e6;using namespace std;int p;
vector <int> a[MAXN];
int b[MAXM];
int f[MAXM],ans,n,m;void input(int &r)
{int t = getchar();while (!isdigit(t)) t = getchar();r = 0;while (isdigit(t)) r = r * 10+t-'0', t = getchar();
}int main()
{//freopen("F:\\rush.txt", "r", stdin);int t;input(t);for (int q = 1; q <= t; q++){for (int i = 0; i <= 1000000; i++)//a[0]也要clear!a[i].clear();ans = 0;input(n); input(m); input(p);for (int i = 1; i <= n; i++){int x;input(x);a[(i%p)].push_back(x);}for (int j = 0; j <= m - 1; j++)input(b[j]);f[0] = 0; f[1] = 0;for (int i = 1; i <= m - 1; i++)//获取失配函数,b数组下表是从0开始的。{int j = f[i];while (j && b[j] != b[i]) j = f[j];f[i + 1] = b[j] == b[i] ? j + 1 : 0;}for (int i = 0; i <= p - 1; i++)//给p个数列找匹配数目{int j = 0, len = a[i].size();for (int k = 0; k <= len - 1; k++){while (j && a[i][k] != b[j]) j = f[j];if (a[i][k] == b[j])j++;if (j == m){ans++;j = f[j];}}}printf("Case #%d: %d\n", q, ans);}return 0;
}
转载于:https://www.cnblogs.com/AWCXV/p/7632169.html
【37.68%】【hdu 5918】Sequence I相关推荐
- 【HDU - 5918 】Sequence I (数组(字符串)匹配问题,可选KMP)
题干: Mr. Frog has two sequences a1,a2,⋯,ana1,a2,⋯,an and b1,b2,⋯,bmb1,b2,⋯,bm and a number p. He want ...
- 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- 大数加法【HDU 1002】
大数加法模板 一般的加法只要int类型的两数直接相加即可,大一点的数可以设为long long类型,而超过长整型的数则属于大数问题了,大数加法其实也比较简单,利用数组实现就可以啦: 主要思想如下: ( ...
- 【HDU/算法】最短路问题 杭电OJ 2544 (Dijkstra,Dijkstra+priority_queue,Floyd,Bellman_ford,SPFA)
最短路径问题是图论中很重要的问题. 解决最短路径几个经典的算法 1.Dijkstra算法 单源最短路径(贪心),还有用 priority_queue 进行优化的 Dijkstra 算法. 2.bell ...
- 【HDU 1735】字数统计(贪心,有缺陷的一道题)
题目分析: 告知有m段,第一行一定带领一段,所以要找出另外m-1段. 由于题目要求最少有多少字被破坏,所以我们要找出的m-1段要求被破损的最少,即是"好"的部分最多. 满足一段开头 ...
- 【 HDU - 5093】Battle ships(匈牙利算法,二分图匹配)
题干: Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission ...
- 【HDU - 1455】Sticks (dfs + 剪枝)
题干: George took sticks of the same length and cut them randomly until all parts became at most 50 un ...
- 【HDU - 4006】The kth great number (优先队列,求第k大的数)
题干: Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to wri ...
- 【HDU - 4217 】Data Structure? (线段树求第k小数)
题干: Data structure is one of the basic skills for Computer Science students, which is a particular w ...
最新文章
- SQL中获取刚插入记录时对应的自增列的值
- 设计模式 之美 -- 建造者模式
- java模拟一个军队作战_战区级联合作战仿真推演系统
- 用命令行CMD .bat 相关操作 如: 创建快捷方式 复制文件等
- socket缓冲区以及阻塞模式
- ArcGIS中实现将圆16等分
- CF 1529C Parsa‘s Humongous Tree
- android+坐标类,Android Path和PathMeasure类的使用之获取圆弧上的坐标值
- Python天天美味(17) - open读写文件
- winform根据字符串生成HTML静态页
- 解决Deepin开机锁屏状态下能够使用触控板而解锁之后无法使用触控板的BUG
- 电驴搜索服务器正在连接,电驴 电驴连接不上服务器-完美教程资讯
- 汇川H3U实现圆弧/直线插补
- txt改成java没反应_为什么我的TXT文档后戳换成java就打不开呢
- 线性表(链式存储结构)C语言
- 音频相概念扫盲——声音处理的过程
- 《番茄工作法图解》全书笔记
- ctypeh里的函数c语言,大满贯平台网址网址-官网首页
- windows系统巨型帧问题
- android视图工具栏,Android studio预览视图工具栏不见了