Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3770    Accepted Submission(s): 1485

Problem Description
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   ....经典题

思路清晰,就不多说啦!...

就是求目标串在主串中出现几次,即匹配几次,,.

代码:

 /*@coder 龚细军*/
#include<iostream>
#include<string>
#include<vector>
using namespace std; int main()
{
int t,i,j;
string pw,ps;
cin>>t;
while(t--)
{
i=,j=-;
cin>>pw>>ps; /*pw作为目标串,ps作为主串*/
int lenpw=pw.length();
int lenps=ps.length();
vector<int>next(lenpw+,);
next[i]=-;
while(i<lenpw)
{
if(j==-||pw[i]==pw[j])
{
++i;
++j;
if(pw[i]!=pw[j])
next[i]=j;
else
next[i]=next[j];
}
else
j=next[j];
}
i=-,j=-;
int ans=;
while(i<lenps)
{
if(j==-||pw[j]==ps[i])
{
++i;
++j;
}
else
j=next[j];
if(j==lenpw)
ans++;
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. 【jq】c#零基础学习之路(1)Hello World!
  2. Redis 读后小感
  3. 关于StartCoroutine的简单线程使用
  4. hdu4992 Primitive Roots(所有原根)
  5. Java——Image 图片切割
  6. [oracle] ORA-08002:序列XXXXXXX.CURRVAL尚未在此进程中定义
  7. Installing Ruby 1.9.3 on Ubuntu 12.04 Precise Pengolin (without RVM)
  8. JS 原型链图形详解
  9. 【笔记】Loadrunner添加OS类型为Windows的服务器(Win7)
  10. vue的双向绑定原理及实现
  11. RxJava(六) retryWhen操作符实现错误重试机制
  12. Configure the MySQL account associate to the domain user via MySQL Windows Authentication Plugin
  13. Microsoft Visual C++ Compiler for Python 2.7真正下载地址
  14. 小白的CTF学习之路7——内存与硬盘
  15. 关于 flask 实现数据库迁移以后 如何根据创建的模型类添加新的表?
  16. guxh的python笔记三:装饰器
  17. [转帖]nvidia nvlink互联与nvswitch介绍
  18. Windows 10 正式版原版ISO镜像
  19. [Objective-C语言教程]指针(15)
  20. cgic实现输入文件名,打开文件的功能

热门文章

  1. [MAC OS ] UserDefaults
  2. Rsync服务介绍与配置
  3. tornado基础入门(一)——简单了解tornado
  4. 第二十三章 springboot + 全局异常处理
  5. 第十四章 springboot + profile(不同环境读取不同配置)
  6. 网页IE轻松调用VLC播放器实现监控(组件+方法大全)【转】
  7. 敏捷开发方法XP的12个最佳实践
  8. c++ 编译时检测结构体大小的的宏定义写法
  9. 如何在MVC的ActionLink中应用Resource文件
  10. Mysql创建多列唯一索引Sql