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