1. HDU 1711 Number Sequence

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 10007 int a[],b[N],next[N];
int n,m; void getnext()
{
next[] = -;
int i = ,j = -;
while(i<m)
{
if(j == - || b[i] == b[j])
{
++i,++j;
if(b[i] != b[j])
next[i] = j;
else
next[i] = next[j];
}
else
j = next[j];
}
} int kmp()
{
int i = -,j = -;
while(i<n && j<m)
{
if(j == - || a[i] == b[j])
i++,j++;
else
j = next[j];
}
if(j == m)
return i-j+;
return ;
} int main()
{
int t,i,res;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
scanf("%d",&a[i]);
for(i=;i<m;i++)
scanf("%d",&b[i]);
if(n<m)
{
printf("-1\n");
continue;
}
getnext();
res = kmp();
if(res)
printf("%d\n",res);
else
printf("-1\n");
}
return ;
}

代码2:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 10007 int a[],b[N],next[N];
int n,m; void getnext()
{
next[] = -;
int i = ,j = -;
while(i<m-)
{
if(j == - || b[i] == b[j])
next[++i] = ++j;
else
j = next[j];
}
} int kmp()
{
int i = -,j = -;
while(i<n && j<m)
{
if(j == - || a[i] == b[j])
i++,j++;
else
j = next[j];
}
if(j == m)
return i-j+;
return ;
} int main()
{
int t,i,res;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
scanf("%d",&a[i]);
for(i=;i<m;i++)
scanf("%d",&b[i]);
if(n<m)
{
printf("-1\n");
continue;
}
getnext();
res = kmp();
if(res)
printf("%d\n",res);
else
printf("-1\n");
}
return ;
}

2.POJ 3461 Oulipo

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 10007 char a[],b[N];
int next[N];
int n,m,cnt; void getnext()
{
next[] = -;
int i = ,j = -;
while(i<m)
{
if(j == - || b[i] == b[j])
next[++i] = ++j;
else
j = next[j];
}
} void kmp()
{
int i = -,j = -;
while(i<n && j<m)
{
if(j == - || a[i] == b[j])
i++,j++;
else
j = next[j];
if(j == m)
{
cnt++;
j = next[j];
}
}
} int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%s",b);
scanf("%s",a);
n = strlen(a);
m = strlen(b);
getnext();
cnt = ;
kmp();
printf("%d\n",cnt);
}
return ;
}

基础KMP两道的更多相关文章

  1. 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制

    你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...

  2. 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester

    这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...

  3. 两道面试题,带你解析Java类加载机制

    文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...

  4. 【转】两道面试题,带你解析Java类加载机制(类初始化方法 和 对象初始化方法)

    本文转自 https://www.cnblogs.com/chanshuyi/p/the_java_class_load_mechamism.html 关键语句 我们只知道有一个构造方法,但实际上Ja ...

  5. ACM/ICPC 之 欧拉回路两道(POJ1300-POJ1386)

    两道有关欧拉回路的例题 POJ1300-Door Man //判定是否存在从某点到0点的欧拉回路 //Time:0Ms Memory:116K #include<iostream> #in ...

  6. ACM/ICPC 之 Floyd范例两道(POJ2570-POJ2263)

    两道以Floyd算法为解法的范例,第二题如果数据量较大,须采用其他解法 POJ2570-Fiber Network //经典的传递闭包问题,由于只有26个公司可以采用二进制存储 //Time:141M ...

  7. ACM/ICPC 之 SPFA范例两道(POJ3268-POJ3259)

    两道以SPFA算法求解的最短路问题,比较水,第二题需要掌握如何判断负权值回路. POJ3268-Silver Cow Party //计算正逆最短路径之和的最大值 //Time:32Ms Memory ...

  8. ACM/ICPC 之 两道dijkstra练习题(ZOJ1053(POJ1122)-ZOJ1053)

    两道较为典型的单源最短路径问题,采用dijkstra解法 本来是四道练习题,后来发现后面两道用dijkstra来解的话总觉得有点冗余了,因此暂且分成三篇博客(本篇以及后两篇). ZOJ1053(POJ ...

  9. 两道二分coming~

    第一道:poj 1905Expanding Rods 题意:两道墙(距离L)之间架一根棒子,棒子受热会变长,弯曲,长度变化满足公式( s=(1+n*C)*L),求的是弯曲的高度h. 首先来看这个图: ...

随机推荐

  1. (三)play之yabe项目【数据模型】

    (三)play之yabe项目[数据模型] 博客分类: 框架@play framework   创建项目 play new yabe What is the application name? [yab ...

  2. 常用 windows运行命令

    winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr--------windows更新程序 wscript--- ...

  3. Android项目实战(九):CustomShapeImageView 自定义形状的ImageView

    一个两年前出来的第三方类库,具有不限于圆形ImageView的多种形状ImageView,项目开发必备 github下载地址:https://github.com/MostafaGazar/Custo ...

  4. 认识Runtime1

    认识Runtime1 什么是id? id在objc.h中的定义如下: typedef struct objc_object *id; 那么什么是objc_object呢? objc_object在ob ...

  5. iOS设计模式之迭代器模式

    迭代器模式 基本理解 迭代器模式(Iterrator):提供一个方法顺序访问一个聚合对象中的各个元素,而又不暴露该元素的内部表示. 当你访问一个聚合对象,而且不管这些对象是什么都需要遍历的时候,你就应 ...

  6. 基础学习day12--多线程一线程之间的通信和常用方法

    一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信.    等待/唤醒机制涉及的方法:    1. wait():让线程处于冻结状态,被wa ...

  7. 朝花夕拾-android 一个注册新用户时,多步填写用户资料的框架

    源码地址:http://git.oschina.net/zj2012zy/Android-Demo/tree/master/AndroidDemo/setpregister 效果如下: 基本思路: 1 ...

  8. IE下实现打印功能

    先贴代码: <html> <head> <style type="text/css"> *{margin:0px;padding:0px;} . ...

  9. window10系统安装oracle11g时遇到INS-13001环境不满足最低要求

    机器安装了window10系统,之前有次安装oracle11g是成功了.但是机器后来固态硬盘坏了,又坏了个后,还是win10系统安装oracle11g时,出现INS-13001环境不满足最低要求,郁闷 ...

  10. c++基础回顾

    #include <iostream> #include <vector> #include <string> int main(int argc, const c ...