http://poj.org/problem?id=3461

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 42698   Accepted: 17154

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

 
 
①:hash 搞   对于y的任意子串x   hash(x,y)=hash[y]-hash[x]*p^(y-x+1)
 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; #define p 147
#define ull unsigned long long
char s1[],s2[];
ull n,ss1,ss2,P;
ull lens,hs[],ans; inline void Get_hash()
{
lens=;
for(ull i=;i<=ss1;i++)
lens=lens*p+s1[i]-'A';
}
inline void Get_hs()
{
for(ull i=;i<=ss2;i++)
hs[i]=hs[i-]*p+s2[i]-'A';
}
inline ull Q_pow(ull a,ull b)
{
ull ret=,base=a;
for(;b;b>>=,base*=base)
if(b&) ret*=base;
return ret;
}
inline ull Check(ull x,ull y)
{
return hs[y]-hs[x-]*P;
} int main()
{
for(cin>>n;n--;ans=)
{
scanf("%s%s",s1+,s2+);
ss1=strlen(s1+),ss2=strlen(s2+);
Get_hash(); Get_hs(); P=Q_pow(p,ss1);
for(ull i=ss1;i<=ss2;i++)
if(lens==Check(i-ss1+,i)) ans++;
cout<<ans<<endl;
}
return ;
}

② kmp

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; int n,ss1,ss2;
char s1[],s2[];
int lens,next[],ans; inline void Get_next()
{
int j=;next[]=;
for(int i=;i<=ss1;i++)
{
for(;j>&&s1[i]!=s1[j+];) j=next[j];
if(s1[i]==s1[j+]) j++;
next[i]=j;
}
} inline void kmp()
{
int j=;
for(int i=;i<=ss2;i++)
{
for(;j>&&s2[i]!=s1[j+];) j=next[j];
if(s1[j+]==s2[i]) j++;
if(j==ss1) ans++,j=next[j];
}
} int main()
{
for(cin>>n;n--;ans=)
{
scanf("%s%s",s1+,s2+);
ss1=strlen(s1+),ss2=strlen(s2+);
Get_next(); kmp();
printf("%d\n",ans);
}
return ;
}

POJ——T 3461 Oulipo的更多相关文章

  1. POJ 题目3461 Oulipo(KMP)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26479   Accepted: 10550 Descript ...

  2. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

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

  4. POJ 3461 Oulipo 【KMP统计子串数】

    传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

  5. POJ 3461 Oulipo

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  6. POJ 3461 Oulipo[附KMP算法详细流程讲解]

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  7. POJ 3461 Oulipo(模式串在主串中出现的次数)

    题目链接:http://poj.org/problem?id=3461 题意:给你两个字符串word和text,求出word在text中出现的次数 思路:kmp算法的简单应用,遍历一遍text字符串即 ...

  8. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

  9. poj 3461 Oulipo,裸kmp

    传送门 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32373   Accepted: 13093 Desc ...

随机推荐

  1. What is x86 Conforming Code Segment?

    SRC=Microprocessor Based Systems SRC=Computer Architecture: A Quantitative Approach

  2. [Python] Boolean Or "Mask" Index Arrays filter with numpy

    NumPy Reference: Indexing Integer array indexing Boolean array indexing Note: The expression a < ...

  3. 数据格式转换 (三)Office文档转HTML

         HTML Filter 是由北京红樱枫软件有限公司根据HTML Ver 4.01/CSS式样,研制和开发的MS Office系列文档到HTML转换的通用程序库.便于用户实现对多种文档的统一管 ...

  4. swift具体解释之八---------------下标脚本

    swift具体解释之八-----下标脚本 下标脚本 能够定义在类(Class).结构体(structure)和枚举(enumeration)这些目标中.能够觉得是訪问对象.集合或序列的快捷方式.不须要 ...

  5. 七牛用户搭建c# sdk的图文讲解

    Qiniu 七牛问题解答 问题描写叙述:非常多客户属于小白类型. 可是请不要随便喷七牛的文档站.由于须要一点http的专业知识才干了解七牛的api文档.如今我给大家弄个c# sdk的搭建步骤 问题解决 ...

  6. shell 脚本去掉月份和天数的前导零

    #!/bin/sh # # shell 脚本去掉月份和天数的前导零 # 前面填 1 变成百位数,然后减 100 # 去掉前导零的通用方法 $((10#$(date +%m))) # 把字符串分割成数组 ...

  7. nyoj--19--擅长排列的小明(dfs)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难 ...

  8. orm 通用方法——DeleteModel 主键删除

    定义代码: /** * 描述:删除对象 * 作者:Tianqi * 日期:2014-09-17 * param:model 对象实例,包含主键 * return:int 受影响行数 * return: ...

  9. 8种提升 ASP.NET Web API 性能的方法

  10. Codefroces B. Hamming Distance Sum

    Genos needs your help. He was asked to solve the following programming problem by Saitama: The lengt ...