HDU 5918 KMP/模拟

Sequence I

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1013    Accepted Submission(s): 393

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中国大学生程序设计竞赛(长春)-重现赛

题意:

题解:

kmp

 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 #include<bits/stdc++.h>
 8 #include<map>
 9 #include<set>
10 #include<cmath>
11 #include<queue>
12 #include<bitset>
13 #include<math.h>
14 #include<vector>
15 #include<string>
16 #include<stdio.h>
17 #include<cstring>
18 #include<iostream>
19 #include<algorithm>
20 #pragma comment(linker, "/STACK:102400000,102400000")
21 using namespace std;
22 #define  A first
23 #define B second
24 const int mod=1000000007;
25 const int MOD1=1000000007;
26 const int MOD2=1000000009;
27 const double EPS=0.00000001;
28 //typedef long long ll;
29 typedef __int64 ll;
30 const ll MOD=1000000007;
31 const int INF=1000000010;
32 const ll MAX=1ll<<55;
33 const double eps=1e-5;
34 const double inf=~0u>>1;
35 const double pi=acos(-1.0);
36 typedef double db;
37 typedef unsigned int uint;
38 typedef unsigned long long ull;
39 int f[2000100];
40 void get(int *p,int m)
41 {
42     int j=0;
43     f[0]=f[1]=0;
44     for(int i=1;i<m;i++)
45     {
46         j=f[i];
47         while(j&&p[j]!=p[i]) j=f[j];
48         if(p[i]==p[j]) f[i+1]=j+1;
49         else f[i+1]=0;
50     }
51 }
52 int kmp(int *s,int *p,int n,int m)
53 {
54     int num=0;
55     int j=0;
56     for(int i=0;i<n;i++)
57     {
58         while(j&&p[j]!=s[i]) j=f[j];
59         if(s[i]==p[j]) j++;
60         if(j==m) num++;
61     }
62     return num;
63 }
64 int s[2000100],p[2000100],t[2000100];
65 int n,m,q;
66 int main()
67 {
68     int T;
69     scanf("%d",&T);
70     for(int k=1;k<=T;k++)
71     {
72         scanf("%d %d %d",&n,&m,&q);
73         memset(t,0,sizeof(t));
74         memset(p,0,sizeof(p));
75         for(int i=0;i<n;i++)
76             scanf("%d",&t[i]);
77         for(int i=0;i<m;i++)
78             scanf("%d",&p[i]);
79         get(p,m);
80         int ans=0;
81         for(int i=0;i<q;i++)
82         {
83             int num=0;
84             for(int j=i;j<n&&i+(m-1)*q<n;j+=q)
85                 s[num++]=t[j];
86             ans+=kmp(s,p,num,m);
87         }
88         printf("Case #%d: %d\n",k,ans);
89     }
90     return 0;
91 }
92 /*
93 KMP 处理
94 *

模拟

  1 /******************************
  2 code by drizzle
  3 blog: www.cnblogs.com/hsd-/
  4 ^ ^    ^ ^
  5  O      O
  6 ******************************/
  7 #include<bits/stdc++.h>
  8 #include<map>
  9 #include<set>
 10 #include<cmath>
 11 #include<queue>
 12 #include<bitset>
 13 #include<math.h>
 14 #include<vector>
 15 #include<string>
 16 #include<stdio.h>
 17 #include<cstring>
 18 #include<iostream>
 19 #include<algorithm>
 20 #pragma comment(linker, "/STACK:102400000,102400000")
 21 using namespace std;
 22 #define  A first
 23 #define B second
 24 const int mod=1000000007;
 25 const int MOD1=1000000007;
 26 const int MOD2=1000000009;
 27 const double EPS=0.00000001;
 28 //typedef long long ll;
 29 typedef __int64 ll;
 30 const ll MOD=1000000007;
 31 const int INF=1000000010;
 32 const ll MAX=1ll<<55;
 33 const double eps=1e-5;
 34 const double inf=~0u>>1;
 35 const double pi=acos(-1.0);
 36 typedef double db;
 37 typedef unsigned int uint;
 38 typedef unsigned long long ull;
 39 int t;
 40 int a[1000006];
 41 int b[1000006];
 42 int d[1000006];
 43 map<int,int> mp;
 44 vector<int > ve[1000006];
 45 int n,m,p;
 46 int main()
 47 {
 48     while(scanf("%d",&t)!=EOF)
 49     {
 50         for(int l=1; l<=t; l++)
 51         {
 52             mp.clear();
 53             scanf("%d %d %d",&n,&m,&p);
 54             for(int i=1; i<=n; i++)
 55                 scanf("%d",&a[i]);
 56             int jishu=1;
 57             for(int i=1; i<=m; i++ )
 58             {
 59                 ve[i].clear();
 60                 scanf("%d",&b[i]);
 61                 if(mp[b[i]]==0)
 62                 {
 63                     mp[b[i]]=jishu;
 64                     d[jishu]=i;
 65                     jishu++;
 66                 }
 67             }
 68             int minx=10000000;
 69             int what=0;
 70             for(int i=1; i<=n; i++)
 71             {
 72                 if(mp[a[i]])
 73                 {
 74                     ve[mp[a[i]]].push_back(i);
 75                 }
 76             }
 77             for(int i=1; i<jishu; i++)
 78             {
 79                 if(minx>ve[i].size())
 80                 {
 81                     minx=ve[i].size();
 82                     what=i;
 83                 }
 84             }
 85             int sum=0;
 86             for(int i=0; i<ve[what].size(); i++)
 87             {
 88                 int st=ve[what][i]-(d[mp[a[ve[what][i]]]]-1)*p;
 89                 int ed=ve[what][i]+(m-d[mp[a[ve[what][i]]]])*p;
 90                 int zha=1;
 91                 int flag=0;
 92                 if(st<1||ed>n)
 93                     flag=1;
 94                 if(flag==0)
 95                 {
 96                     for(int j=st; j<=ed; j+=p)
 97                     {
 98                         if(a[j]!=b[zha++])
 99                         {
100                             flag=1;
101                             break;
102                         }
103                     }
104                 }
105                 if(flag==0)
106                     sum++;
107             }
108             printf("Case #%d: %d\n",l,sum);
109         }
110     }
111     return 0;
112 }
时间: 2024-10-02 09:18:02

HDU 5918 KMP/模拟的相关文章

hdu 4964 Emmet(模拟)

题目链接:hdu 4964 Emmet 题目大意: 给定语句,按照语法翻译并输出. 解题思路:用递归模拟文法分析,主要注意几点: 括号并且的情况:(fuck)(you) 括号嵌套的情况:((fuck.you)) 优先输出id,然后是class(题目中有说) 乘法的部分:fuck*2>you*3 (每次执行fuck时,you的地方同样被执行了3次) 其他跑出样例基本没问题,具体看代码. #include <cstdio> #include <cstring> #include

HDU 4891 简单模拟

The Great Pan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1035    Accepted Submission(s): 355 Problem Description As a programming contest addict, Waybl is always happy to take part in vario

hdu 4194(模拟)

符合三者之一的则不满足规定,求不满足规定的个数.直接模拟. 1.被同一个人审查多次 2.被和自己同一组织的审查 3.被审查次数不等于k 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-28 17:36 5 * Filename :

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

hdu 1686 KMP模板

1 // hdu 1686 KMP模板 2 3 // 没啥好说的,KMP裸题,这里是MP模板 4 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 10 using namespace std; 11 12 const int MAX_N = 1000008; 13 const int MAX_M = 10008; 14 char T

HDU 2100 Lovekey 模拟26进制

Problem Description XYZ-26进制数是一个每位都是大写字母的数字. A.B.C.-.X.Y.Z 分别依次代表一个0 ~ 25 的数字,一个 n 位的26进制数转化成是10进制的规则如下 A0A1A2A3-An-1 的每一位代表的数字为a0a1a2a3-an-1 ,则该XYZ-26进制数的10进制值就为 m = a0 * 26^(n-1) + a1 * 26^(n-2) + - + an-3* 26^2 + an-2*26 + an-1 一天vivi忽然玩起了浪漫,要躲在学校

Cyclic Nacklace HDU 3746 KMP 循环节

Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len]-len的差即是循环部分的长度. 这个是重点.这个题目自己开始没有想明白,看的博客,推荐这个. 代码实现 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const i

HDU 5918 Sequence I KMP

Sequence I 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. In