HDU 1686Oulipo ———————Hash or KMP

数字IC后端实战:如何用ICC搞定复杂模块的floorplan与powerplan(附避坑指南) 本文深入解析数字IC后端设计中复杂模块的floorplan与powerplan实战技巧,重点介绍多边形模块边界处理、MTCMOS电源网络优化及存储器摆放陷阱规避。通过Synopsys ICC工具的具体操作示例和项目实战经验,帮助工程师高效解决设计难题,提升芯片性能与可靠性。 阅读详情

Oulipo



Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22302    Accepted Submission(s): 8608



Problem 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


 


Recommend

lcy   |   We have carefully selected several similar problems for you:   1358  3336  3746  2203  3068 

 


Statistic |  Submit |  Discuss | Note





Hash_code:

Problem : 1686 ( Oulipo )     Judge Status : Accepted
RunId : 25951470    Language : G++    Author : 311709001223
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN=1e6+7;
const int MAXM=1e4+7;
const int base=233;
char str1[MAXM],str2[MAXN];
ull Hash[MAXN];
int t;
int id[30],idx;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s %s",str1,str2);
        int ans=0;
        int len1=strlen(str1);
        int len2=strlen(str2);
        if(len1>len2)
        {
            printf("%d\n",ans);
            continue;
        }
        ull sh1=0;//字串的hash值
        ull sh2=0;//母串的hash值,先计算前len1 长度的hash值
        ull hb=1;
        for(int i=0;i<len1;i++) sh1=sh1*base+str1[i]-'A';
        for(int i=0;i<len1;i++) sh2=sh2*base+str2[i]-'A';
        for(int i=0;i<len1;i++) hb*=base;
        for(int i=0;i+len1<=len2;i++)
        {
            if(sh1==sh2)    ans++;
            sh2=sh2*base -(str2[i]-'A')*hb + (str2[i+len1]-'A');
        }
        printf("%d\n",ans);
    }
    return 0;
}



KMP_code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1e6+7;
int slen,plen;
char s[MAXN],p[MAXN];
int Next[MAXN];
void preKMP(char x[],int m,int kmpNext[])
{
    int i,j;
    j=kmpNext[0]=-1;
    i=0;
    while(i<m)
    {
        while(-1!=j && x[i]!=x[j])  j=kmpNext[j];
        if(x[++i]==x[++j])  kmpNext[i]=kmpNext[j];
        else    kmpNext[i]=j;
    }
}
int KMP(char x[],int m,char y[],int n)
{
    int i,j;
    int ans=0;
    preKMP(x,m,Next);
    i=j=0;
    while(i<n)
    {
        while(-1!=j && y[i]!=x[j])  j=Next[j];
        i++;j++;
        if(j>=m)
        {
            ans++;
            j=Next[j];
        }
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s %s",p,s);
        plen=strlen(p);
        slen=strlen(s);
        printf("%d\n",KMP(p,plen,s,slen));
    }
    return 0;
}

Pareto 优化 (Pareto Optimization) 算法详解及案例分析 Pareto 最优解是指在多目标优化问题中,无法通过改进一个目标而不损害其他目标的解。具体来说,对于一个解x∗x^*x∗,如果不存在另一个解xxx使得所有目标函数fixf_i(x)fi​x都不劣于fix∗f_i(x^*)fi​x∗,且至少有一个目标函数fjxf_j(x)fj​x优于fjx∗f_j(x^*)fj​x∗,则x∗x^*x∗是 Pareto 最优解。 阅读详情

相关推荐

基于Docker的D435i相机与IMU联合标定实战指南

本文详细介绍了基于Docker的D435i相机与IMU联合标定实战指南,涵盖kalibr镜像构建、IMU标定全流程、双目相机标定技巧及联合标定结果验证。通过Docker容器化部署,简化了复杂依赖环境,提升标定效率与精度,适用于机器人视觉与SLAM应用。

weixin_30872337的博客 239

hdu1686:KMP板子

题目链接 题目大意就是给你两个字符串,求出第一个字符串在第二个字符串中出现的次数。 如果我们暴力匹配的话,复杂度是 len(first) * len(second) 对于题目给的 1e4 * 1e6 显然暴力不可取, 这里就用到 KMP 。 说到 KMP 最难理解的就是 next 数组了下面给出了 next 数组的详细求法。 我们先预设两个指针,一个指向 i = 0 ,一个指向 j = -1 .应...

lifehappy 342

硬件电路(22)-TTL和232切换电路

工作中会遇到TTL和232电平的通信的状态,为了实现一种便捷的切换方式,参考了一些文章,提供一种电路方便大家使用。

剑藏锋的博客 1862

HDU 1686(Oulipo)

题意:给定文本串 a 和 模式串 b ,求 b 在 a 中出现过多少次。 分析:KMP裸题,稍微改动一下就可以 了 代码: #include<cstdio> #include<cstring> #include<iostream> using namespace std; const int N = 1E6+10; const int M...

Joker & Liar的博客 209

如何用SPSS做logistic回归

详细到只有作者本人能看懂的SPSS逻辑回归育儿经

qq_44794961的博客 2万+

PyCharm旧版本下载地址

快捷找到PyCharm历史版本下载地址

piaopiaolanghua的专栏 4434

哈希-hdu1686(b串中a串出现的次数)

#include &lt;iostream&gt; #include &lt;cstdio&gt; #include &lt;cstring&gt; using namespace std; typedef unsigned long long ull; const int m=31; const int maxn=1e6+5; char a[maxn],b[maxn]; int la,lb; ...

xqx1343002589的博客 489

【5G NB-IoT NTN】3GPP R17 NB-IoT NTN介绍_nbiot ntn

你的支持,我的动力;祝各位前程似锦,offer不断,步步高升!!!” />你的支持,我的动力;祝各位前程似锦,offer不断,步步高升!!!更多资料点击此处获qu!!

2401_83703866的博客 3165

Hdu 1686 Oulipo(KMP入门)

传送门 题意:给你一个长度为W的模式串(1 ≤ |W| ≤ 10,000)和一个长度为T的文本串(|W| ≤ |T| ≤ 1,000,000),问你这个模式串在文本串中出现了几次,注意样例里面的这一组样例AZA AZAZAZA 输出的是3而不是2,重复的也算一次。 分析:显然根据HDU一贯的作风(套路),这题我们肯定不能用朴素的暴力匹配,那样的话,复杂度是O(M*N),肯定会超时,其实这题就是...

不期而遇 225

【教程】BM1684X-32边缘计算盒子 网络配置教程

BM1684X边缘计算盒子网络配置教程

ShiMetaPi的博客 714

阿波罗开发板硬件架构与STM32模块化设计详解

嵌入式开发板的模块化设计是提升硬件复用性、降低迭代成本的关键技术路径。其核心在于底板与核心板的电气解耦与功能分层,依托STM32系列MCU(如F429/F767)的丰富外设资源与引脚复用能力,实现信号完整性、电源域隔离与热管理的系统级协同。该架构显著支撑工业HMI、智能家居网关、便携医疗设备等典型场景,尤其在RGB LCD驱动、QSPI Flash内存映射、多协议通信(CAN/RS485/USB)及低功耗唤醒(WK_UP按键)等工程实践中展现出高可靠性与易调试性。本文围绕阿波罗开发板展开深度解析,覆盖最小系

weixin_42510201的博客 64

KMP~~算法+HDU1686

KMP算法主要应用于字符串的匹配。 对于正常的字符串模式匹配,主串长度为m,子串为n,时间复杂度会到达O(m*n),而如果用KMP算法,复杂度将会减少线型时间O(m+n)。   设主串为ptr="ababaaababaa";,要比较的子串为a=“aab”;   KMP算法用到了next数组,然后利用next数组的值来提高匹配速度,我首先讲一下next数组怎么求,之后再讲匹配方式。   ...

gao506440410的博客 279

华为OD机试双机位C卷-分披萨 (Py/Java/C/C++/Js/Go)

华为OD机试(机考)双机位C卷 【分披萨】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为od机试 - 分披萨。

qq_45776114的博客 1396

【哈希】HDU 1686 Oulipo

哈希和KMP两种解法。颜文字缺失qwq

L_X_ 327

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Oulipo HDU 1686 (哈希或KMP)

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...

Top_Spirit的博客 254

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

poj3461 && hdu1686 Oulipo KMP && HASH哈希

Oulipo   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, m...

cloudy_happy的博客 232

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

hdu1686 - Oulipo (kmp / hash)

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,...

xtu 2018的博客 275

【KMP】HDU 1686 Oulipo

哈希和KMP两种解法。颜文字缺失qwq

L_X_ 240

HDU 1686 Oulipo(hash)

题目连接 思路 kmp板子题,才知道hash这操作,试试水。 代码 #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long const ull seed = 163; ull h[1000005], p[1000005]; void init...

←_← 186

Oulipo HDU - 1686(哈希或KMP)匹配字符串

题意:字符串匹配:寻找字符串S中,字符串T出现的次数 思路:KMP或哈希 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...

zeng_jun_yv的博客 330

KMP-Oulipo HDU - 1686

KMP-Oulipo HDU - 1686 题目: 求模式串在待匹配串中的出现次数。 Input 第一行是一个数字T,表明测试数据组数。之后每组数据都有两行: 第一行为模式串,长度不大于10,000; 第二行为待匹配串,长度不大于1,000,000。(所有字符串只由大写字母组成) Output 每组数据输出一行结果。 Sample Input 4 ABCD ABCD SOS SOSOSOS CDC...

njuptACMcxk的博客 202

hdu -- 1686 Oulipo (简单Hash,(操作字符))

简单哈希–操作字符串       题目链接:  HDU 1686 Oulipo 题意: 给你两个字符串str1,str2. 问:str1在str2中出现了多少次? 实际上这一题是KMP的简单的模板题。这里用hash同样可以更加简单的解决 Hash的三个步骤: 1,初始化 ( init() ) 2,建哈希 ( make_hash() ) 3,取哈希 ( get_hash() ) /* ****** Q:代码里为什么没有取模? ****** A

weixin_45705885的博客 418

【HDU 1686 Oulipo】KMP/HASH

HDU1686 题意就是求B串在A串中的出现次数(可重叠 依旧是利用next数组,当某次匹配完成之后,将ans++,然后把j跳转到next[j],原理类似上一题的不匹配的情况,省略不必要的查找。 HDU1686代码 #include&amp;lt;stdio.h&amp;gt; #include&amp;lt;iostream&amp;gt; #include&amp;lt;string.h&amp;gt; using namesp...

lajiyuan的博客 322

HDU1686 Oulipo(扩展KMP)

题意:给出两个串w和t,求w在t中出现的次数,不同次数可以覆盖。 思路:可以做一次扩展KMP,求出以t的每一位作起点与w的匹配长度,然后扫一遍结果,达到w的长度就加一。 #include #include #include #include #include #include #include #include using namespace std; typedef long

hcx11333的博客 434

Oulipo (哈希)

HDU - 1686    求模式串在待匹配串的出现次数。 Input 第一行是一个数字T,表明测试数据组数。 之后每组数据都有两行:第一行为模式串,长度不大于10000;第二行为待匹配串,长度不大于1000000。所有字符串只由大写字母组成。 Output 每组数据输出一行结果。 Sample Input 4 ABCD ABCD ABA ABABABA CDCDCDC C...

qq_42759455的博客 268

Oulipo-HDU-1686 哈希

这个题我之前用kmp板子写过一次,这次开始学习哈希,再上一个单哈希的方法,就当入门模板吧。 思路: 算出子字符串的哈希值,然后遍历主字符串枚举起点,算出和相同长度子串的哈希值,如果值相同则答案加1。 代码如下: #include&amp;lt;iostream&amp;gt; #include&amp;lt;cstdio&amp;gt; #include&amp;lt;fstream&amp;gt; #include&amp

Apollo的博客 387
上一篇: POJ 1200 Crazy Search——————Hash算法
下一篇: Pizza Separation——————数学
陶鸿杰
博客等级 码龄9年 93粉丝 298原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值