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];

就能继续找匹配了。

记住就好。不然每次都想好烦。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <vector>
  4. const int MAXN = 2e6;
  5. const int MAXM = 2e6;
  6. using namespace std;
  7. int p;
  8. vector <int> a[MAXN];
  9. int b[MAXM];
  10. int f[MAXM],ans,n,m;
  11. void input(int &r)
  12. {
  13. int t = getchar();
  14. while (!isdigit(t)) t = getchar();
  15. r = 0;
  16. while (isdigit(t)) r = r * 10+t-'0', t = getchar();
  17. }
  18. int main()
  19. {
  20. //freopen("F:\\rush.txt", "r", stdin);
  21. int t;
  22. input(t);
  23. for (int q = 1; q <= t; q++)
  24. {
  25. for (int i = 0; i <= 1000000; i++)//a[0]也要clear!
  26. a[i].clear();
  27. ans = 0;
  28. input(n); input(m); input(p);
  29. for (int i = 1; i <= n; i++)
  30. {
  31. int x;
  32. input(x);
  33. a[(i%p)].push_back(x);
  34. }
  35. for (int j = 0; j <= m - 1; j++)
  36. input(b[j]);
  37. f[0] = 0; f[1] = 0;
  38. for (int i = 1; i <= m - 1; i++)//获取失配函数,b数组下表是从0开始的。
  39. {
  40. int j = f[i];
  41. while (j && b[j] != b[i]) j = f[j];
  42. f[i + 1] = b[j] == b[i] ? j + 1 : 0;
  43. }
  44. for (int i = 0; i <= p - 1; i++)//给p个数列找匹配数目
  45. {
  46. int j = 0, len = a[i].size();
  47. for (int k = 0; k <= len - 1; k++)
  48. {
  49. while (j && a[i][k] != b[j]) j = f[j];
  50. if (a[i][k] == b[j])
  51. j++;
  52. if (j == m)
  53. {
  54. ans++;
  55. j = f[j];
  56. }
  57. }
  58. }
  59. printf("Case #%d: %d\n", q, ans);
  60. }
  61. return 0;
  62. }

【37.68%】【hdu 5918】Sequence I的更多相关文章

  1. 【hdu 5918】Sequence I(KMP)

    给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...

  2. 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】

    利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

    Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...

  5. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  7. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  8. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

  9. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

随机推荐

  1. 对生产者和消费者问题的另一个解决办法是使用QWaitCondition(封装好了wakeOne,wakeAll,而且与QReadWriteLock对接,几乎是万能的办法)

    对生产者和消费者问题的另一个解决办法是使用QWaitCondition,它允许线程在一定条件下唤醒其他线程.其中wakeOne()函数在条件满足时随机唤醒一个等待线程,而wakeAll()函数则在条件 ...

  2. nginx服务器,访问时如何不直接显示index.php,而是显示目录

    版权声明:m_nanle_xiaobudiu https://blog.csdn.net/m_nanle_xiaobudiu/article/details/79502787 效果: 这里,我使用的是 ...

  3. MFC切换图片防止闪烁

    处理WM_ERASEBKGND消息,在消息处理函数中return TRUE;

  4. PatentTips - Modified buddy system memory allocation

    BACKGROUND Memory allocation systems assign blocks of memory on request. A memory allocation system ...

  5. matlab 正则表达式

    regexprep Replace text using regular expression collapse all in page Syntax newStr = regexprep(str,e ...

  6. UVALive - 3977 Summits (BFS染色)

    题目大意:坑爹的题目.题意那么难理解. 讲的就是,假设该点是山顶的话(高度为h).那么以该点为中心,往外辐射.走高度大于h-d的点,到达不了还有一个比它高的点 这就提示了,高度要从大到小排序,依次以高 ...

  7. 【C++竞赛 F】yyy的三角形

    时间限制:2s 内存限制:32MB 问题描述 yyy对三角形非常感兴趣,他有n个木棍,他正在用这些木棍组成三角形.这时xxx拿了两根木棍过来,xxx希望yyy能给他一根木棍,使得xxx可以组成一个三角 ...

  8. 走进windows编程的世界-----对话框、文本框、button

    1 对话框的分类  2 对话框的基本使用方式  3 对话框资源  4 有模式对话框的使用 int DialogBox( HINSTANCE hInstance, LPCTSTR lpTemplate, ...

  9. web网站如何获取用户的地理位置

    web网站如何获取用户的地理位置 一.总结 一句话总结:通过gps知道用户的经度和纬度,然后通过经度和纬度在在地图(google或者百度)上面显示位置. 1.html5如何通过gps知道用户的经度和纬 ...

  10. 授人玫瑰 手留余香 --纪念python3.2.3官方文档翻译结束

    当你点击看到这篇文章的时候.你已经得到了祝福. 一个来自夜深人静的码农,在2014年5月19号的01:18分.默默为你献上祝福. 希望你.我和他,每个在IT行业中奋斗的人.能找到属于自己一片天空. 在 ...