【CodeForces】471D MUH and Cube Walls KMP
来源:互联网 发布:mac边栏图标大小 编辑:程序博客网 时间:2024/05/17 21:44
传送门:【CodeForces】471D MUH and Cube Walls
题目分析:很容易想到KMP,然后可以进一步的想到如果轮廓能匹配,也就意味着相邻两个匹配的主串 减 模式串的值相同即可匹配。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm> #define rep( i , a , b ) for ( int i = ( a ) ;i < ( b ) ;++ i )#define rev( i , a , b ) for ( int i = ( a ) ;i >= ( b ) ;-- i )#define For( i , a , b ) for ( int i = ( a ) ;i <= ( b ) ;++ i )#define clr( a , x ) memset ( a , x , sizeof a ) typedef long long LL ; const int MAXN = 200005 ;int s[MAXN] ;int p[MAXN] ;int next[MAXN] ;int n , m ;int cmp ( int i , int j , int a[] , int b[] ) {if ( j == 1 ) return 1 ;return a[i] - b[j] == a[i - 1] - b[j - 1] ;}void get_next () {next[1] = 0 ;int j = 0 ;For ( i , 2 , m ) {while ( j && !cmp ( i , j + 1 , p , p ) ) j = next[j] ;if ( cmp ( i , j + 1 , p , p ) ) ++ j ;next[i] = j ;}}void get_kmp () {int ans = 0 ;int j = 0 ;For ( i , 1 , n ) {while ( j && !cmp ( i , j + 1 , s , p ) ) j = next[j] ;if ( cmp ( i , j + 1 , s , p ) ) {++ j ;if ( j == m ) {++ ans ;//printf ( "%d\n" , i ) ;j = next[j] ;}} else j = next[j] ;//printf ( "%d\n" , j ) ;}printf ( "%d\n" , ans ) ;}void solve () {For ( i , 1 , n ) scanf ( "%d" , &s[i] ) ;For ( i , 1 , m ) scanf ( "%d" , &p[i] ) ;get_next () ;get_kmp () ;}int main () {while ( ~scanf ( "%d%d" , &n , &m ) ) solve () ;return 0 ;}
0 0
- codeforces 471D MUH and Cube Walls kmp
- 【CodeForces】471D MUH and Cube Walls KMP
- CodeForces 471D MUH and Cube Walls(KMP匹配)
- CodeForces 471D MUH and Cube Walls KMP
- CodeForces 471D MUH and Cube Walls(KMP)
- Codeforces 471D MUH and Cube Walls【思维+KMP】
- 471D - MUH and Cube Walls (KMP)
- CodeForces 471D MUH and Cube Walls
- codeforces MUH and Cube Walls (kmp)
- 【CODEFORCES】 D. MUH and Cube Walls
- 【CodeForces】471D MUH and Cube Walls KMP或者字符串HASH
- CodeForces 471D MUH and Cube Walls KMP算法+串的取差性质
- 【字符串hash】 codeforces 471D MUH and Cube Walls
- Codeforces 471D MUH and Cube Walls 题解
- Codeforces #269 (Div. 2)D. MUH and Cube Walls(数据结构:KMP)
- Codeforces Round #269 (Div. 2) D题 MUH and Cube Walls(KMP)
- Codeforces Round #269 (Div. 2)(D. MUH and Cube Walls(KMP))
- Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!
- 改变世界的17个等式
- 滤波器设计指标
- 数字信号处理中各种频率关系
- weblogic 监控工具汇总及简介
- 学习FPGA的网站推荐
- 【CodeForces】471D MUH and Cube Walls KMP
- 常见算法在实际项目中的应用
- L5:01 - DP综述
- 配置ssh公钥访问oschina
- 使用接插件需要注意的问题
- hdu 1049
- 再见,孤独的九月;再见,佯装成熟的我们
- L5:02 - 【类型一】Matrix DP(10%)
- html 高手解决 <Iframe></frame>间的退回上一页面问题