Oulipo (KMP出现次数)

The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
  • One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0

KMP板子题

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>using namespace std;
void kmp_pre(char x[],int m,int next[]) {int i,j;j=next[0]=-1;i=0;while(i<m) {while(-1!=j && x[i]!=x[j])j=next[j];next[++i]=++j;}
}
int next[1000010];
int KMP_Count(char x[],int m,char y[],int n) { //x 是模式串,y 是主串int i,j;int ans=0;
//preKMP(x,m,next);kmp_pre(x,m,next);i=j=0;while(i<n) {while(-1!=j && y[i]!=x[j])j=next[j];i++;j++;if(j>=m) {ans++;j=next[j];}}return ans;
}char a[10005];
char b[1000005];
int main() {int T;cin>>T;while(T--) {scanf("%s",a);scanf("%s",b);printf("%d\n",KMP_Count(a,strlen(a),b,strlen(b)));}return 0;
}

 

转载于:https://www.cnblogs.com/Staceyacm/p/10781820.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/450038.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从夫妻吵架中看项目管理

从夫妻吵架中看项目管理&#xff08;很有意思的文章&#xff09; 首先要说明&#xff1a;和老婆吵架无论原因如何&#xff0c;无论结果如何你都是错的&#xff0c;老婆永远是对的。但是我不是神仙&#xff0c;偶尔也要吵架。但是如何让吵架也发挥作用&#xff0c;增进夫妻感情&…

SpringMVC工作原理

大家好&#xff0c;我是IT修真院深圳分院第十一期学员&#xff0c;一枚正直纯洁善良的JAVA程序员。 今天给大家分享一下&#xff0c;修真院官网JAVA任务二的一个知识点&#xff1a;SpringMVC工作原理 1、背景介绍 一&#xff1a;背景介绍 JavaWeb经历的几个变化&#xff1a; 1:…

Android应用开发—如何解决handler的警告:Handler Class Should be Static or Leaks Occur

转自android handler的警告Handler Class Should be Static or Leaks Occur 在使用Handler更新UI的时候&#xff0c;我是这样写的&#xff1a; public class SampleActivity extends Activity {private final Handler mLeakyHandler new Handler() {Overridepublic void hand…

从远程(包括ftp,http等协议)地址获取文件流信息

URL url new URL("ftp://172.18.251.155:8010/recordsImg/2019-01-28/000008_1548649813267.jpg"); MultipartFile multipartFile new MockMultipartFile(fileName,fileName,"", url.openStream());转载于:https://www.cnblogs.com/baihaojie/p/10331134…

shell 数组

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 1&#xff09;定义数组&#xff1a; my_array(1 2 3 4) 也可这样赋值&#xff1a;my_array[4]爱 读取&#xff1a; echo ${my_array[2]…

nodejs 实现文件拷贝

通过4中不通的方式实现对一个文件的拷贝 方式一&#xff1a;readFile 异步一次性读取文件再写入 //异步形式读取文件 function copyFile(url){const extName path.extname(url)const fileName path.basename(url)const dirName path.dirname(url)fs.readFile(url, (err, dat…

国家部委对4G调研:未定给中电信联通发放牌照

一场有关4G牌照发放的论战正在发酵&#xff0c;矛盾的核心在于&#xff0c;除了中移动外&#xff0c;政府是否也会向中电信和联通发放TD-LTE(中国主导的4G标准)牌照 记者 王云辉 雍忠玮 一场围绕4G的新博弈已经白热化。 “多个国家部委正在对4G展开全面调研&#xff0c;但最终如…

Luogu4735 最大异或和

题目蓝链 Description 给你一个序列&#xff0c;你需要支持以下两个操作&#xff1a; A x: 在序列尾部添加一个整数\(x\)&#xff0c;序列的长度增加\(1\)Q l r x: 询问操作&#xff0c;你需要找到一个位置\(p \in [l, r]\)&#xff0c;使得&#xff1a;\(x \bigoplus a_p \big…

Spring-jdbc:JdbcTemplate使用简介

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架. 作为 SpringJDBC 框架的核心, JDBC 模板…

Java多线程编程:变量共享分析(Thread)

Java多线程编程&#xff1a;变量共享分析&#xff08;Thread&#xff09; Java 创建线程的两种方法 此处只简单讲下自己对java多线程变量共享的理解&#xff1a; 按照进程和多线程的原理&#xff0c;同一进程内的多个线程之间的地址空间是共享的&#xff08;除去ThreadLocal&a…

嘉益仕(Litns)带您读懂MES系统:选型篇

自从智能制造概念提出以来&#xff0c;制造执行系统MES在国内掀起了新一波的热潮。众多企业在技术发展、政策导向和自身需要的推动下&#xff0c;纷纷上马MES请添加链接描述项目。 由此也带动了MES软件开发企业的快速发展。一夜之间MES软件开发企业遍地开花&#xff0c;MES产品…

[WPF]xml序列化以及反序列化数据

代码 XML序列化工具类 public static class XMLHelper{/// <summary>/// 将对象序列化为指定的文件名/// </summary>/// <typeparam name"T"></typeparam>/// <param name"obj"></param>/// <param name"fil…

多线程的那点儿事

1. 多线程的那点儿事&#xff08;基础篇&#xff09; 多线程编程是现代软件技术中很重要的一个环节。要弄懂多线程&#xff0c;这就要牵涉到多进程&#xff1f;当然&#xff0c;要了解到多进程&#xff0c;就要涉及到操作系统。不过大家也不要紧张&#xff0c;听我慢慢道来。…

Android应用开发—AsyncTask

摘录自 Android 多线程—–AsyncTask详解 AsyncTask AsyncTask&#xff1a;异步任务&#xff0c;从字面上来说&#xff0c;就是在我们的UI主线程运行的时候&#xff0c;异步的完成一些操作。AsyncTask允许我们的执行一个异步的任务在后台。我们可以将耗时的操作放在异步任务当…

std::shared_ptr之deleter的巧妙应用

本文由作者邹启文授权网易云社区发布。std::shared_ptr一次创建&#xff0c;多处共享&#xff0c;通过引用计数控制生命周期。 实例 在邮箱大师PC版中&#xff0c;我们在实现搜索时&#xff0c;大致思路是这样的&#xff1a; 每一个账号都有一个SearchFlow&#xff0c;搜索开始…

js - 执行上下文和作用域以及闭包

首先&#xff0c;咱们通常被"执行上下文"&#xff0c;"执行上下文环境"&#xff0c;"上下文环境"&#xff0c;"执行上下文栈"这些名词搞混。那我们一一来揭秘这些名字的含义。 这一块一直比较晦涩难懂&#xff0c;还是需要仔细去斟酌斟…

Spring之JDBCTemplate

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 一、Spring对不同的持久化支持&#xff1a; Spring为各种支持的持久化技术&#xff0c;都提供了简单操作的模板和回调 ORM持久化技术模…

从蚂蚁金服实践入手,带你深入了解 Service Mesh

本文整理自蚂蚁金服高级技术专家敖小剑在 QCon 上海 2018 上的演讲。我是来自蚂蚁金服中间件团队的敖小剑&#xff0c;目前是蚂蚁金服 Service Mesh 项目的 PD。我同时也是 Servicemesher 中国技术社区的创始人&#xff0c;是 Service Mesh 技术在国内最早的布道师。我今天给大…

Android应用开发—FragmentManager如何管理fragments

本文主要摘录自Android中使用FragmentManager管理fragments 和 浅谈FragmentManager与fragment之一二事 先讲下自己对fragment的理解&#xff1a; 对于fragment&#xff0c;有太多官方文档和博文来介绍&#xff0c;此处不做转述&#xff1a;我感觉android提供fragment这种组件…