学习:重点理解这句话的意思: 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. prototype原型链继承

    依旧是恶补js基础,上代码: 1.定义父类及父类方法 function Animal(){ this.name = "动物"; } Animal.prototype.eat = f ...

  2. IDE开发<LER-Studio>(2)::登录模块

    软件中写登录模块是为了防止软件的恶意传播,内测阶段可以忽略登录. 以下为登录模块主要源代码: void CLoginDlg::OnBnClickedBtnLogin() { // TODO: Add ...

  3. zabbix 配置

    终于把zabbix配置好了.可能还有待优化   我主要参考了几个链接 http://lnmp.org/install.html   一键安装lnmp http://blog.unix178.com/2 ...

  4. HTML 事件

    1.HTML 全局事件属性 HTML4 的新特性之一就是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某个 HTML 元素时启动一段 JavaScript,在 HTML5 中还增加了一些新 ...

  5. JavaScript获取非行间样式/定义样式

    html节点的样式分为以下几种 (1)浏览器默认样式 (2)引用样式(引用外部css文件的样式.style标签内定义的样式) 引用外部css样式:<link rel="styleshe ...

  6. python連接mysql數據庫

    第一步,安裝mysql數據庫. 這裏我安裝的是mariadb數據庫,版本5.5,並且配置好了字符集.此處不詳細敘述,相信大家沒有問題. 第二步,安裝mysql驅動. 首先說明一下有兩個主要的驅動: m ...

  7. ConfigParser---python

    # !/usr/bin/python # Filename:tcfg.py import ConfigParser config = ConfigParser.ConfigParser() confi ...

  8. SQLite Helper (C#) z

    http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp Introduction I have written a small ...

  9. ARM 的Thumb状态测试

    作为一个使用ARM的学习者,有必要全面了解你的处理器内核.尽管有些内容可能在实际应用中用不到,但是“了解”还是很必要的.Thumb状态,是ARM的一个特色,但是你知道Thumb状态与ARM状态最大的区 ...

  10. cocos2dx 3.3创建新项目 和 VS2012解决方案加载失败问题

     首先创建新项目,步骤如下: 1.进入cocos2d-x-3.3\tools\cocos2d-console\bin目录,按住shift+鼠标右键 2.输入 cocos new 项目名 –p 包名 – ...