hdu1686 - Oulipo (kmp / hash)

本文介绍了一种在目标串中查找模式串的算法,包括KMP算法和哈希算法的实现,探讨了如何快速准确地找到模式串在目标串中的所有出现位置,即使串可以重叠。

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

 

题目大意:

求在目标串中能找到多少模式串,串可以重叠。

这题只要把kmp函数修改一下就行,匹配成功就使ans++并用next数组回跳到下一个匹配位置,直到目标串指针指到末尾。

AC代码:

kmp:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char s[1000005];
char p[10005];
int nex[10005];
void cal_next(int lenp, int lens){
    nex[1] = 0;
    int j;
    for (int i = 2; i <= lenp; i ++){
        j = nex[i - 1];
        while(j > 0 && (j == lens || p[i] != p[j + 1]))j = nex[j];
        if (p[i] == p[j + 1]) j ++;
        nex[i] = j;
    }
}
int kmp(int lenp, int lens){
    int ans = 0;
    int i = 1, j = 1;
    while(j <= lens){
        if (p[i] == s[j])i ++, j ++;
        else if(i > 1)i = nex[i - 1] + 1;
        else j ++;
        if (i > lenp)ans ++, i = nex[i - 1] + 1;
    }
    return ans;
}
int main()
{
    int t;
    cin >> t;
    while(t --){
        scanf("%s", p + 1);
        scanf("%s", s + 1);
        int lenp = strlen(p + 1);
        int lens = strlen(s + 1);
        cal_next(lenp, lens);
        cout << kmp(lenp, lens) << endl;
    }
    return 0;
}

hash:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef unsigned long long ull;
const int N = 1e6 + 5;
char a[10005];
char b[1000005];
ull f[N], p[N];
int main()
{
    int t;
    cin >> t;
    while(t --){
        scanf("%s", a);
        scanf("%s", b + 1);
        int lena = strlen(a);
        int lenb = strlen(b + 1);
        p[0] = 1;
        for (int i = 1; i <= lenb; i ++){
            f[i] = f[i - 1] * 131 + b[i] - 'a' + 1;
            p[i] = 131 * p[i - 1];
        }
        ull ha = 0;
        for (int i = 0; i < lena; i ++)ha = ha * 131 + a[i] - 'a' + 1;
        int cnt = 0;
        //cout << ha << endl;
        for (int i = 1; i + lena - 1 <= lenb; i ++){
            int j = i + lena - 1;
            //cout << f[j] - f[i - 1] * p[j - i + 1] << endl;
            if(f[j] - f[i - 1] * p[j - i + 1] == ha)cnt ++;
        }
        cout << cnt << endl;
    }
    return 0;
}

 

<think>我们正在解决一个关于Hi3861开发板在OpenHarmony环境中编译MQTT相关示例时出现的文件路径错误问题。用户提到了几个关键路径:hi3861_hdu_iot_application,paho_mqtt,MQTTPacket,MQTTClient-C,ohos。我们需要根据引用内容来定位问题。根据引用[3],我们得知在Hi3861的示例程序中,有一个features列表,其中包含MQTT相关的示例(如"26_paho.mqtt.embedded-c:app")。用户可能是在编译这个示例时遇到了文件路径错误。常见问题(引用[1])包括:1.Sources源文件路径不正确2.Include_dirs头文件路径不正确3.JSON语法错误(如缺少逗号)4.注释处理不当因此,我们需要检查与MQTT示例相关的BUILD.gn文件,确保源文件和头文件路径正确,并且没有语法错误。步骤:1.确认MQTT库是否已经正确放置在vendor目录下。通常,第三方库(如paho.mqtt.embedded-c)应该放在vendor的某个子目录中。2.检查示例程序的BUILD.gn文件(如demo/BUILD.gn)中是否正确定义了源文件和头文件路径。3.检查依赖的库(如MQTTPacket、MQTTClient-C)是否在项目中存在,并且路径正确。具体到用户的问题,错误可能是由于在编译26号示例时,系统找不到paho.mqtt.embedded-c库的相关文件。我们需要查看该示例的构建脚本。根据引用[3]中的示例,我们注意到在demo/BUILD.gn中,每个feature对应一个示例程序。因此,26号示例对应的构建目标在features中为"26_paho.mqtt.embedded-c:app"。这个目标应该对应一个名为app.gn的文件,位于26_paho.mqtt.embedded-c目录下。我们需要检查这个app.gn文件中的路径设置。假设项目结构如下:```vendor└──hihope└──hispark_pegasus└──demo├──26_paho.mqtt.embedded-c│├──app.gn│└──...(源文件)└──BUILD.gn```在app.gn中,应该定义了源文件路径和头文件路径。例如:```python#示例app.gn内容import("//build/lite/config/component/lite_component.gni")executable("app"){output_name="app"sources=["paho_mqtt_demo.c",#其他源文件]include_dirs=["//vendor/hihope/hispark_pegasus/demo/26_paho.mqtt.embedded-c",#MQTT库的头文件路径"//vendor/paho.mqtt.embedded-c/MQTTPacket/src","//vendor/paho.mqtt.embedded-c/MQTTClient-C/src",]deps=["//vendor/paho.mqtt.embedded-c:MQTTPacket","//vendor/paho.mqtt.embedded-c:MQTTClient-C",]cflags=["-Wno-unused-variable"]}```同时,在vendor/paho.mqtt.embedded-c目录下,应该有两个库的构建目标(BUILD.gn文件),例如:```python#vendor/paho.mqtt.embedded-c/BUILD.gnstatic_library("MQTTPacket"){sources=["MQTTPacket/src/MQTTConnectClient.c","MQTTPacket/src/MQTTConnectServer.c",...#其他源文件]include_dirs=["MQTTPacket/src"]}static_library("MQTTClient-C"){sources=["MQTTClient-C/src/MQTTClient.c"]include_dirs=["MQTTPacket/src",#依赖MQTTPacket的头文件"MQTTClient-C/src"]deps=[":MQTTPacket"]}```解决方案:1.确认paho.mqtt.embedded-c库已经正确下载并放置在vendor目录下(可能是vendor/paho.mqtt.embedded-c)。2.检查26_paho.mqtt.embedded-c示例的app.gn文件中的include_dirs和deps是否指向正确的路径。3.检查paho.mqtt.embedded-c库的BUILD.gn文件是否存在,且语法正确(如逗号、括号等)。如果用户遇到找不到文件的问题,可能是路径大小写不一致,或者路径层级不对。需要确保在app.gn中引用的路径与实际目录结构一致。另外,根据引用[1],还要检查JSON配置(如果有)和注释符号的使用。如果用户是在修改后的代码中出现问题,请确保在BUILD.gn中取消注释了MQTT示例(即去掉26号前面的#号)。如果问题仍然存在,可以尝试以下步骤:-检查整个项目的目录结构,确保MQTT库的源文件和头文件在正确的位置。-查看编译错误信息,确定是哪个文件找不到,然后根据错误信息调整路径。注意:在OpenHarmony的构建系统中,路径通常从根目录开始(//表示根目录),因此要确保路径从根目录开始正确。例如,如果paho.mqtt.embedded-c库放在vendor目录下,则路径应为:`"//vendor/paho.mqtt.embedded-c/MQTTPacket/src"`。如果用户将库放在其他地方,则需要相应调整路径。最后,如果用户使用的不是hihope的示例,而是自己的项目,那么需要在自己的BUILD.gn中正确配置这些路径。希望这些步骤能帮助用户解决问题。</think>针对Hi3861开发板在编译MQTT相关库时出现的文件路径错误问题,结合引用内容和OpenHarmony开发经验,解决方案如下: ### 问题分析 根据引用[3],MQTT示例(`26_paho.mqtt.embedded-c:app`)在`BUILD.gn`中属于可选编译项。路径错误通常由以下原因导致: 1. **头文件/源文件路径未正确配置**(引用[1]) 2. **MQTT库未正确集成到工程中** 3. **GN构建脚本语法错误**(如缺少逗号或括号) ### 解决方案 #### 步骤1:验证MQTT库路径 1. 确认`paho_mqtt`库已下载并放置在正确目录: ```bash # 标准目录结构参考 vendor/ └── hihope/ └── hispark_pegasus/ ├── demo/ │ └── 26_paho.mqtt.embedded-c/ # MQTT示例目录 └── third_party/ └── paho.mqtt.embedded-c/ # MQTT库源码 ``` 若缺失库文件,需从[官方仓库](https://github.com/eclipse/paho.mqtt.embedded-c)克隆: ```bash git clone https://github.com/eclipse/paho.mqtt.embedded-c vendor/third_party/paho.mqtt.embedded-c ``` #### 步骤2:修正GN构建脚本 在`demo/BUILD.gn`中(引用[3]): ```python lite_component("demo") { features = [ # 启用MQTT示例(移除行首#号) "26_paho.mqtt.embedded-c:app", ] } ``` 在`26_paho.mqtt.embedded-c/app.gn`中检查路径: ```python executable("app") { include_dirs = [ // 添加MQTT库头文件路径 "//vendor/third_party/paho.mqtt.embedded-c/MQTTPacket/src", "//vendor/third_party/paho.mqtt.embedded-c/MQTTClient-C/src", ] sources = [ "mqtt_demo.c", // 添加MQTT库源文件 "//vendor/third_party/paho.mqtt.embedded-c/MQTTPacket/src/*.c", "//vendor/third_party/paho.mqtt.embedded-c/MQTTClient-C/src/*.c" ] # 注意:路径分隔符使用正斜杠/,每行末尾加逗号 } ``` #### 步骤3:检查JSON配置 在`config.json`中确认MQTT组件已声明: ```json { "subsystem": "iot", "components": [ { "component": "paho_mqtt", "features": [] } ] } ``` #### 步骤4:编译验证 执行编译命令,观察输出: ```bash hb build -f ``` 若仍有路径错误,根据报错信息调整`include_dirs`和`sources`中的路径深度。 ### 关键检查点 1. **路径大小写敏感**:Linux系统严格区分大小写,确保`MQTTPacket`等名称拼写准确 2. **依赖库完整性**:检查`third_party/paho.mqtt.embedded-c`是否包含`src`目录 3. **GN语法验证**:所有数组元素以逗号结尾,字符串用双引号包裹(引用[1]) ### 注意事项 - 鸿蒙编译系统要求**绝对路径**(以`//`开头) - 若使用自定义路径,需在`build/lite/config/subsystem/iot.gni`中注册新路径 - 网络功能需提前配置WiFi连接(引用[3]中`19_wificonnect`示例) ### 相关问题 1. Hi3861开发中如何正确配置第三方库的GN构建脚本? 2. OpenHarmony编译出现`undefined reference to`错误应如何排查?[^1] 3. 如何为Hi3861移植新的物联网协议库(如CoAP)? 4. MQTT连接失败时有哪些调试手段? [^1]: 引用自站内文档《FS_Hi3861鸿蒙开发板编译问题排查》 [^2]: 引用自Hi3861芯片规格说明 [^3]: 引用自《Hi3861 OpenHarmony嵌入式应用入门》环境配置示例
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值