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. HTML5按键打开摄像头和拍照

    HTML5实现按键打开摄像头和拍照 步骤: 1.创建一个打开摄像头按钮的标签.video标签.拍照的按钮标签.画布 2.实现打开摄像头的功能 3.实现拍照功能   具体实现代码: <!DOCTY ...

  2. [JLOI2011]飞行路线(分层图)

    [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在 n 个城市设有业务,设这些城市分别标记为 0 到 n−1 ,一共有 m ...

  3. 题解 P2068 【统计和】

    这是一道单点修改,区间查询的线段树. 需要实现的操作有三个:建树,更新与查询. 首先,线段树用结构体维护,如下: struct node { int l, r; int val; } tree[max ...

  4. codevs——T2806 红与黑

    http://codevs.cn/problem/2806/  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 白银 Silver 题解       题目描述 Descriptio ...

  5. error C2440: “static_cast”: 无法从“LRESULT (__thiscall CTextProgressCtrl::* )(UINT,LPCTSTR)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)

    转自原文 error C2440 “static_cast” 无法从“void (__thiscall C* )(void)... error C2440: “static_cast”: 无法从“LR ...

  6. 【 D3.js 入门系列 --- 2.1 】 关于怎样选择,插入,删除元素

    本人的个人博客首页为: http://www.ourd3js.com/  ,csdn博客首页为:http://blog.csdn.net/lzhlzz/. 转载请注明出处,谢谢. 在D3.js中,选择 ...

  7. C#文件拖放至窗口的ListView控件获取文件类型

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. style="background-image: url(__HOMEPAGE__/views/IMJ2V2/images/banner2.jpg)"

    style="background-image: url(__HOMEPAGE__/views/IMJ2V2/images/banner2.jpg)" 一.问题 backgroun ...

  9. CentOS 与Ubuntu 安装软件包的对比

    工作需要开始转向centos,简单记录软件包安装 wget不是安装方式 他是一种下载软件类似与迅雷 如果要下载一个软件 我们可以直接 wget 下载地址 ap-get是ubuntu下的一个软件安装方式 ...

  10. Python列表插入字典(转)

    https://blog.csdn.net/qq_29721419/article/details/70310183