学习:重点理解这句话的意思: next[j]会告诉我们从哪里开始匹配     模板题。

Oulipo

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

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
 
Source
 
Recommend
lcy

 #include"stdio.h"
#include"string.h"
int next[],num;
char a[],b[];
int lena,lenb;
void get_next()
{
int i,j;
i=;next[]=-;j=-;
while(i<lenb)
{
if(j==-||b[i]==b[j])
{
i++;
j++;
if(b[i]==b[j])
{
next[i]=next[j];
}
else
{
next[i]=j;
}
}
else
{
j=next[j];
}
}
}
void kmp()
{
int i,j;
i=;
j=;
num=;
while(i<lena)
{
if(a[i]==b[j]||j==-)
{
i++;
j++;
}
else
{
j=next[j];
}
if(j==lenb)//当j==Lenb next[j]会告诉我们从哪里开始匹配
{
num++;
j=next[j];
}
}
printf("%d\n",num);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
getchar();
while(n--)
{
gets(b);
gets(a);
lena=strlen(a);
lenb=strlen(b);
get_next();
kmp();
}
}
return ;
}

HDU-1686 Oulipo的更多相关文章

  1. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  2. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  3. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

  4. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  5. hdu 1686 Oulipo kmp算法

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...

  6. HDU 1686 Oulipo(KMP变形求子串出现数目(可重))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...

  7. hdu 1686 Oulipo (kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. #include <iostream> #i ...

  8. HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)

    HDU题目 POJ题目 求目标串s中包含多少个模式串p KMP算法,必须好好利用next数组,, (kmp解析)——可参考 海子的博客  KMP算法 //写法一: #include<string ...

  9. HDU 1686 Oulipo(kmp)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  10. hdu 1686 Oulipo (kmp)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

随机推荐

  1. O-C相关05:方法的封装.

    前言:在 OC 中进行封装, 就是实现设置实例变量和获取实例变量数据的方法, 常常称为 setter 方法和 getter 方法. 或称为 get set 读写器. 1,setter 方法 sette ...

  2. struts2基于Convention插件的约定映射使用

    一.首先说明一点:所谓的基于Convention插件的约定优于配置的使用,并不是严格意义上的零配置,struts.xml文件并不能完全舍弃. 获得Convention插件功能,所必需的jar包有:|a ...

  3. Effective C++ 学习总结

    摒弃C的做法采用C++的实现方式 以const和inline代替define 以iostream流代替stdio 以new和delete代替 C++风格注释 内存管理 new和delete, new[ ...

  4. MongoDB笔记(一)MongoDB概述和安装

    概述 关键词:关系数据库.非关系数据库 关系数据库: 关系数据库,是建立在关系数据库模型基础上的数据库,借助于集合代数等概念和方法来处理数据库中的数据.目前主流的关系数据库有oracle.SQL.ac ...

  5. leetcode problem 11 Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  6. eclipse 启动 出现Failed to create the Java Virtual Machine" 解决方案

    方法如下: 找到eclipse目录下的eclipse.ini,可以看到如下内容:-startupplugins/org.eclipse.equinox.launcher_1.1.0.v20100507 ...

  7. 服务器慢 mysql-bin.000001文件占满磁盘的原因与解决

    发现 VPS 服务器上的网站反应超级慢,简单的重启.重启各主要服务,发现mysql 的反应极其不正常. 一方面是问题,这与站点访问量有关.开始时从mysql 的配置文件 my.cnf 考虑,但志文工作 ...

  8. C# return dynamic/anonymous type value as function result

    function: public static dynamic GetAppSecret() { //string[] result = new string[] { "", &q ...

  9. ORACLE 常用系统函数

    1.  字符类 1.1  ASCII(c ) 函数  和CHR( i )      ASCII 返回一个字符的ASCii码,其中c表示一个字符;CHR 返回ascii码值i 所对应的字符 . 如: S ...

  10. COGS 577 蝗灾 [CDQ分治入门题]

    题目链接 昨天mhr神犇,讲分治时的CDQ分治的入门题. 题意: 你又一个w*w正方形的田地. 初始时没有蝗虫. 给你两个操作: 1. 1 x y z: (x,y)这个位置多了z只蝗虫. 2. 2 x ...