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 ...
随机推荐
- 欢迎访问微先锋vXianFeng官方博客
欢迎访问微先锋vXianFeng官方博客,专注微商城.P2P理财.山寨矿机平台研究与开发!
- [洛谷P1156][codevs1684]垃圾陷阱
题目大意:一头奶牛掉进了深度为d的坑里,现在有g个垃圾在特定时刻ti扔进来.奶牛可以吃垃圾以获得体力,吃第i个垃圾能获得mi的体力,也可以堆放垃圾以逃生,第i个垃圾高度为hi.当高度≥d时奶牛成功逃生 ...
- request.getxxxxxx()的使用方法
request.getSchema() 可以返回当前页面使用的协议,http 或是 https; request.getServerName() 可以返回当前页面所在的服务器的名字; request. ...
- Centos7不修改默认交换分区下添加交换分区
交换分区介绍 Linux系统中的交换分区是当物理内存(RAM)被充满时,作为物理内存的缓存来使用. 当系统需要更多的内存资源而物理内存已经充满,内存中不活跃的页就会被移动到交换分区上. 交换分区位于硬 ...
- main()函数的形参
main函数中的第一个参数argc代表的是向main函数传递的参数个数,第二个参数argv数组代表执行的程序名称和执行程序时输入的参数 #include <stdio.h> int mai ...
- CMSIS-RTOS 时间管理之时间延迟Time Delay
时间管理 Time Management 此RTOS除了可以把你的应用代码作为线程运行,它还可以提供一些时间服务功能,使用这些功能你就可以访问RTOS的一些系统调用. 时间延迟Time Delay 在 ...
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
- 多client并发登录
//LoginClient.java package mySocket; import java.io.BufferedReader; import java.io.InputStreamReader ...
- vue17 $watch 监听数据变化
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ssh 免密及加密远程脚本实现
windows_host文件路径:C:\Windows\System32\drivers\etc ssh-copy-id -i ~/.ssh/id-rsa.pub root@xxxxxxx 免密验证操 ...