Oulipo

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

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
 #include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
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 index_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)//匹配失败,寻找数组中可以匹配的位置
{
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();
index_KMP();
}
}
return ;
}
 

HDU 1686 Oulipo(优化的KMP)的更多相关文章

  1. 题解报告:hdu 1686 Oulipo(裸KMP)

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

  2. 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 ...

  3. HDU - 1686 Oulipo KMP匹配运用

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

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

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

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

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

  6. hdu 1686 Oulipo kmp算法

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

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

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

  8. hdu 1686 Oulipo (kmp)

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

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

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

随机推荐

  1. HDU1510 White rectangles( 乱搞 O(n^3) )题解

    思路: 友谊赛的时候一直想到了,但是没想出来怎么遍历才能找到所有矩阵,卡住了. 这里讲一下完整思路:我们用一个num[i][j]表示第i行第j列每一列连续的白色格子数量,然后我们定义一个MIN,并且每 ...

  2. Ubuntu 14.04安装gnuplot 解决Terminal type set to 'unknown'问题 简易命令教程

    参考: 照猫画虎学gnuplot之折线图 gnuplot 入门教程 1 gnuplot安装,及error:terminal type set to 'unknown'的解决 安装 sudo apt-g ...

  3. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  4. Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip

    http://codeforces.com/contest/811/problem/C 题意: 给出一行序列,现在要选出一些区间来(不必全部选完),但是相同的数必须出现在同一个区间中,也就是说该数要么 ...

  5. c++ 判断容器是否为空

    #include <iostream> #include <vector> #include <string> using namespace std; int m ...

  6. install ros-indigo-filters

    CMake Warning at /opt/ros/indigo/share/catkin/cmake/catkinConfig.cmake: (find_package): Could not fi ...

  7. lua if 流程控制

    Lua认为false和nil为假,true和非nil为真. 要注意的是Lua中 0 为 true --[ 为 true ] ) then print("0 为 true") end ...

  8. javaScript 真经 小感 this 指向

    编程世界只存在两种基本元素:一个是数据.一个是代码. (能写代码算入门,能处理复杂场景或者数据算合格,能不变应万变是不朽) 最流行的编程思想莫过于面向对象编程,因为面向对象编程思想把数据和代码结合成统 ...

  9. Flex 布局的各属性取值解释

    Flex布局是一种弹性布局.布局样式比较灵活,大多数情况下可以替代float,而且不会脱离文档里流. Flex中定义了两个轴线,一个主轴一个副轴,这个概念你可以想想屏幕坐标系(X轴向右,Y轴向下),F ...

  10. C# winform实现右下角弹出窗口结果的方法

    using System.Runtime.InteropServices; [DllImport("user32")] private static extern bool Ani ...