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. WebService通讯技术的CXF框架问题

    WebService通讯技术的CXF框架问题 严重: Servlet /cxf_rs_spring threw load() exception java.lang.ClassCastExceptio ...

  2. 监控memcached服务

    #!/bin/bash #监控memcached服务 printf "del key\r\n" | nc 127.0.0.1 11211 &>/dev/null #使 ...

  3. Could not create connection to database server. Attempted reconnect 3 times. Giving up.错误

    项目是基于springboot框架,昨天从git上pull代码之后也没有具体看更改的地方,结果运行的时候就报错了. java.sql.SQLNonTransientConnectionExceptio ...

  4. 【APP自动化】Appium Android 元素定位方法 原生+H5

    参考资料: http://blog.csdn.net/vivian_ljx/article/details/54410024

  5. Eclipse导出JavaDoc(并解决中文乱码问题)

    一. 使用Eclipse生成注释文档 使用eclipse生成文档(javadoc)主要有三种方法: 1,在项目列表中按右键,选择Export(导出),然后在Export(导出)对话框中选择java下的 ...

  6. java流1----InputStream、OutputStream、Reader、Writer

    字节流和字符流 顾名思义,字节流就是操作的是字节,字符流操作的就是字符.其中字节流又可以分为字节输入流(InputStream)和字节输出流(OutputStream).同样的字符流也可以分为字符输入 ...

  7. POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks

    http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15680   ...

  8. vue19 组建 Vue.extend component、组件模版、动态组件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Create an ASP.NET Core web app in Visual Studio Code

    https://www.microsoft.com/net/core#windowscmd https://download.microsoft.com/download/B/9/F/B9F1AF57 ...

  10. js如何生成[n,m]的随机数(整理总结)

    js如何生成[n,m]的随机数(整理总结) 一.总结 一句话总结: // max - 期望的最大值 // min - 期望的最小值 parseInt(Math.random()*(max-min+1) ...