Oulipo HDU - 1686
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.
InputThe 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.
OutputFor 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 不会kmp的我mmp
网上的题解全是kmp
我觉得hash暴力也应该也行 实践证明 暴力hash是可以的
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 1e4 + ;
const int seed = ;
ull HASH, s[maxn*], p[maxn*];
char a[maxn], b[maxn*], ans[maxn*];
void init() {
p[] = ;
for (int i = ; i < maxn ; i++)
p[i] = p[i - ] * seed;
}
int main() {
init();
int t;
scanf("%d", &t);
while(t--) {
scanf("%s%s", a, b );
int lena = strlen(a), lenb = strlen(b);
HASH = ;
for (int i = ; i < lena ; i++)
HASH = HASH * seed + a[i];
int top = ;
s[] = ;
int sum = ;
for (int i = ; i < lenb ; i++) {
ans[top++] = b[i];
s[top] = s[top - ] * seed + b[i];
if ( top >= lena && s[top] - s[top - lena]*p[lena] == HASH ) sum++;
}
printf("%d\n", sum);
}
return ;
}
外加一个kmp写法吧 毕竟多学点东西肯定没有错
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 1e4 + ;
int nxt[maxn], lena, lenb, ans;
char a[maxn], b[maxn * ];
void Get_nxt() {
int i = , j = - ;
nxt[] = -;
while(i < lena) {
if (j == - || a[i] == a[j] ) {
++i, ++j;
nxt[i] = j;
} else j = nxt[j];
}
}
void kmp() {
int i = , j = ;
Get_nxt();
while(i < lenb) {
if (j == - || b[i] == a[j]) {
++i, ++j;
if (j == lena) ans++;
} else j = nxt[j];
}
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
ans = ;
scanf("%s%s", a, b);
lena = strlen(a);
lenb = strlen(b);
kmp();
printf("%d\n", ans);
}
return ;
}
Oulipo HDU - 1686的更多相关文章
- Oulipo - HDU 1686 (KMP模板题)
题目大意:题目叙述很多,其实只看输入输出也能明白什么意思,给两个串W,T, 判断T串中包含几个串W. 分析:还是基础的KMP应用....................... 直接上代码. === ...
- Oulipo HDU 1686 KMP模板
题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...
- 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 ...
- HDU - 1686 Oulipo KMP匹配运用
id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- hdu 1686 Oulipo KMP匹配次数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...
- HDU 1686 - Oulipo - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...
- hdu 1686 Oulipo kmp算法
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...
- HDU 1686 Oulipo(KMP变形求子串出现数目(可重))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...
随机推荐
- Java学习笔记五:Java中常用的运算符
Java中常用的运算符 运算符是一种“功能”符号,用以通知 Java 进行相关的运算.譬如,我们需要将变量 score 的值设置为 20 ,这时候就需要一个“=”,告诉程序需要进行赋值操作. Java ...
- 03---Nginx配置文件
#启动子进程程序默认用户#user nobody;#一个主进程和多个工作进程.工作进程是单进程的,且不需要特殊授权即可运行:这里定义的是工作进程数量worker_processes 1; #全局错误日 ...
- javaweb 项目部署到tomcat
1.准备环境 1.安装 mysql 2.安装java 环境 3.使用idea打war包 点击 apply 生成war包 将war包传到tomcat的webapp目录下,进入tomcat安装目录,进入b ...
- Linq中dbSet 的查询
1.Find:按照关键字的ID号来查询(速度快) 如: ADShiTi aDShiTi = db.ADShiTis.Find(id); 2.FirstOrDefault:根据部分条件查询,显示最前的一 ...
- 标志寄存器PSW和汇编条件转移指令解释
标志寄存器PSW 标志寄存器PSW(程序状态字寄存器PSW) 标志寄存器PSW是一个16为的寄存器.它反映了CPU运算的状态特征并且存放某些控制标志.8086使用了16位中的9位,包括6个状态标志 ...
- Long Short-Term Memory (LSTM)
Long Short-Term Memory (LSTM) Outline Background LSTM Network Extended LSTM LST ...
- Windows server 2012 R2开机进入cmd,关闭后黑屏问题
原因分析: 因为自己在卸载IIS的时候,不小心卸载了.net framework,系统没有了图形界面(由完整模式Full变为了核心模式core),需要重新恢复.net framework4.5. 解决 ...
- JavaScript 面向对象 原型(prototype) 继承
1.对象的概念:无需属性的集合,属性可以为数值,对象或函数,ECMAscript中没有类的概念,这点是javascript与其他面向对象(OO)语言不同的地方. //创建一个自定义对象 var per ...
- 团队项目-第十次scrum 会议
时间:11.6 时长:20分钟 地点:主235教室走廊 工作情况 团队成员 已完成任务 待完成任务 解小锐 完成多种招聘方式的逻辑编写 陈鑫 实现游戏的存档功能 李金奇 添加多种招聘方式等功能 王辰昱 ...
- intelliJ idea 使用技巧&方法
导入的项目查看svn地址:在项目上右键--subversion--relocate可以看到以前对应的svn地址. 重要的几个快捷键的使用方式: (1) alt+insert 成员变量封装 ...