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.pswp.cn/news/450038.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/450038.shtml
英文地址,請注明出處:http://en.pswp.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這種組件…