#269(div2) D. MUH and Cube Walls

题意:2个序列A,B,B可以自身全部加减某个数字,问B和A匹配的个数

思路:不管怎样,B序列中相邻2个数之间的差是不变的,然后KMP。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=200005;
 4
 5 int aa[N],a[N];
 6 int bb[N],b[N];
 7 int n,m;
 8 int Next[N];
 9
10 void getnext()
11 {
12     int j, k;
13     j = 0; k = -1; Next[0] = -1;
14     while(j < m)
15         if(k == -1 || b[j] == b[k])
16             Next[++j] = ++k;
17         else
18             k = Next[k];
19
20 }
21
22 int KMP_Count()
23 {
24     int ans = 0;
25     int i, j = 0;
26
27     if(n == 1 && m == 1)
28     {
29         if(a[0] == b[0])
30             return 1;
31         else
32             return 0;
33     }
34     getnext();
35     for(i = 0; i < n; i++)
36     {
37         while(j > 0 && a[i] != b[j])
38             j = Next[j];
39         if(a[i] == b[j])
40             j++;
41         if(j == m)
42         {
43             ans++;
44             j = Next[j];
45         }
46     }
47     return ans;
48 }
49
50 int main(){
51     scanf("%d%d",&n,&m);
52     for(int i=0;i<n;i++) scanf("%d",&aa[i]);
53     for(int i=0;i<m;i++) scanf("%d",&bb[i]);
54     for(int i=0;i<n-1;i++) a[i]=aa[i+1]-aa[i];
55     for(int i=0;i<m-1;i++) b[i]=bb[i+1]-bb[i];
56     n--;m--;
57     if(m==0){
58         cout<<n+1<<endl;return 0;
59     }
60     cout<<KMP_Count()<<endl;
61 }
时间: 2024-08-24 22:45:25

#269(div2) D. MUH and Cube Walls的相关文章

Codeforces Round #269 (Div. 2) D. MUH and Cube Walls KMP

D. MUH and Cube Walls Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. T

#269(div2) C. MUH and House of Cards

题意:给出N,问可以组装成不同的层. 思路:发现第一层的mod3=2,第二层的mod3=1,第三层的mod3=0,第四层的mod3=2......然后我们判断N是否足够建立该层最少的所需,第i层所需=3*n*(n-1)/2+2*n,for循环里的i也要开longlong ,不然超时,别问我为什么知道. 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 int main(){ 6 ll n;

#269(div2) B. MUH and Important Things

题意:有n个数字,代表第i个的难度为a[i],问是否有三种不同的序列使得难度从低到高 思路:那么难度相同的对肯定要>=2,然后瞎搞,嗯,瞎搞就行了 1 #include<bits/stdc++.h> 2 using namespace std; 3 struct node 4 { 5 int x; 6 int wei; 7 }a[2003]; 8 bool cmp(node p,node q) 9 { 10 return p.x<q.x; 11 } 12 int main() 13

D - MUH and Cube Walls

D. MUH and Cube Walls Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. T

codeforces MUH and Cube Walls

题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得 a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k 就说b串在a串中出现过!最后输出b串在a串中出现几次! 思路: KMP变形!如何转换成KMP求解呢? 举一个例子说明一下: a: 5 10 8 10 11 9 11 12 10 15 b: 4 2 4 5 3 根据题意 a中的 10 8 10 11 9 与 b是匹配的, 11 9 11 12 10跟b也是匹配的! 如何将b串以及

CodeForces - 471D MUH and Cube Walls

CodeForces - 471D 记录差分,利用kmp对分别除去了第一个数的两个数组进行匹配 注意特判模式串长度为一的情况 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 const int maxn = 2e5 + 10; 6 int ans, n, m; 7 8 void find_substring(int pattern[], int text[]) { 9 vector<int

【CodeForces】471D MUH and Cube Walls KMP或者字符串HASH

想到两点就行: 1.相邻项相减,处理出相对高度,这样如果pattern或者text增加的话,就没问题了 2.KMP匹配O(n) HASH的话 ,我WA在第25组数据了,听说如果改为大素数取模就能AC KMP AC了 但是好奇怪我的KMP模板难道有问题?? 先贴KMP ac 代码 //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring>

cf386(div2)大一狗ACM之路

#cf386(div2)总结#前两题很顺利的做了出来, c题扔了, D题wrong了5发才A掉.A题签到题, 但是想多了, 代码写的有点长了. 找被整除最小值*7.B题 读题读了一会, 读完了就有思路了, 1A. 字符串问题, 从后往前两个两个的放到新的字符串里, 一个从最左, 一个从最右, 模拟指针扫着放, 最后特判会不会扫到一起.C题跳了没看, 最后做完了D题回来看了一眼没什么思路 日后再说.D题, 恩.. 两个多小时都用在这题上面了, 20分钟的时候做完了B之后就一直再啃D题, 暴力判断啊

CF #262 (DIV2) C . Present (二分答案)

output standard output Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on