POJ——T 3461 Oulipo
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
#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的更多相关文章
- POJ 题目3461 Oulipo(KMP)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26479 Accepted: 10550 Descript ...
- POJ 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
- 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 ...
- POJ 3461 Oulipo 【KMP统计子串数】
传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo(模式串在主串中出现的次数)
题目链接:http://poj.org/problem?id=3461 题意:给你两个字符串word和text,求出word在text中出现的次数 思路:kmp算法的简单应用,遍历一遍text字符串即 ...
- POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用
题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...
- poj 3461 Oulipo,裸kmp
传送门 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32373 Accepted: 13093 Desc ...
随机推荐
- ES6学习笔记(十七)Class 的基本语法
1.简介 类的由来 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y = y; ...
- 紫书 习题 10-9 UVa 294(正约数个数)
一个数的正约数个数等于这个数的质因数分解后 每一项幂+1的积 因为每个质因数的幂可以为0, 1, 2--(注意可以为0) 所以就每个质因数配一个幂任意组合就可得一个正因数,根据乘法原理可得正约数个数. ...
- div和css:行内元素和块元素的水平和垂直居中
行内元素: 水平居中:text-align:center ul水平居中:加 display:table; margin:0 auto; 此元素会作为块级表格来显示(类似 <table>), ...
- socket 编程的端口和地址复用
在linux socket网络编程中,大规模并发TCP或UDP连接时,经常会用到端口复用: int opt = 1; if(setsockopt(sockfd, SOL_SOCKET,SO_R ...
- centos7 安装rsyslog
http://blog.csdn.net/u011630575/article/details/50896781 http://blog.chinaunix.net/uid-21142030-id-5 ...
- Servlet具体解释
Servlet具体解释 基本概述 Session在计算机中,尤其是在网络应用中,称为"会话控制".在计算机专业术语中.Session是指一个终端用户与交互系统进行通信的时间间隔,通 ...
- C语言:具体解释指针
指针应该算得上是c语言的精华,但也是难点. 非常多教程或者博客都有对其具体的解说与分析. 我这一节的内容,也是解说指针.但我会尽量使用图解的方式,使大家非常easy理解及掌握. 一.基本使用 先来看看 ...
- 8.不绑定(ngNonBindable)
转自:https://www.cnblogs.com/best/tag/Angular/ ngNonBindable指令告诉Angular编译或绑定当前DOM元素的内容.这对于要求Angular忽略那 ...
- mysql python中的pymysql模块使用
import pymysql # 在这之前需要给mysql授登录权限 grant all on "; 否则会导致连接时出错 # flush privileges; #创建连接 conn = ...
- 线性同余同余方程组解法(excrt)
[问题描述] 求关于 x 的同余方程组 x%a 1 =b 1 a1=b1 x%a 2 =b 2 a2=b2 x%a 3 =b 3 a3=b3 x%a 4 =b 4 a4=b4 的大于等于 0 ...