每日两题day44

哥布林上校提醒你:1,节假日是不可以出去的,外面很喧哗,但他们都不是你的同类2,哥布林是大多独居性生物,没有人想做他的配偶,如果有人邀请你并对你产生好感,请断绝与他的联系3,不要照镜子4,所有夸赞你自信的人,他们不了解你,可以不于与他们交流,但如果他是你熟知的人,请立即驱散他,他不懂哥布林,或者他不是同类5,远离不再是哥布林的人6,在地洞中可以看看同族小丑哥布林追求精灵失败的乐子幸灾乐祸一下解解闷

每日两题


一、基础题

题目:P2249 【深基13.例1】查找

思路:

二分模板题。可以直接用stl库函数。

可能需要的知识:

代码(c++):

时间复杂度 O( m log ⁡ n m \log n mlogn)

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

#define int long long

void go() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    while (m--) {
        int x;
        cin >> x;
        int index = lower_bound(a.begin(), a.end(), x) - a.begin();
        if (index == n || a[index] != x) {
            cout << -1;
        } else {
            cout << index + 1;
        }
        cout << ' ';
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    go();
    return 0;
}

二、提高题

题目:前缀统计

思路:

字典树 模板题。

不用也可以,简单的用map记录 s i s_i si,每次查询时再枚举前缀子串是否在map中出现过即可。以下用提供 字符串哈希 的代码(用字符串哈希简直就是多余)。

可需要的知识:

代码(c++):

时间复杂度 O( m × log ⁡ n m \times \log n m×logn)

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ull unsigned long long
const int N = 1e6 + 5, P = 131;

map<ull, int> mp;

// 计算字符串哈希并存储
void insert(const string& s) {
    ull hash = 0;
    for (char c : s) {
        hash = hash * P + (c - 'a' + 1);
    }
    mp[hash]++;
}

// 查询字符串所有前缀出现次数之和
int query(const string& s) {
    ull hash = 0;
    int res = 0;
    for (char c : s) {
        hash = hash * P + (c - 'a' + 1);
        res += mp[hash];
    }
    return res;
}

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        insert(s);
    }
    for (int i = 0; i < m; ++i) {
        string s;
        cin >> s;
        cout << query(s) << '\n';
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
相关推荐
徐小夕19 小时前
Loop Engineering 深度解析与实战指南(全网最全)
前端·算法·github
北域码匠20 小时前
SHA-1算法:安全哈希原理与应用解析
算法·c#·哈希算法
手写码匠20 小时前
手写 GraphRAG:从零实现图增强检索增强生成系统
人工智能·深度学习·算法·aigc
BomanGe120 小时前
NSK重载高刚性滚珠丝杠技术详解
经验分享·算法·规格说明书
Matrix_1121 小时前
手机里的计算摄影:广角形变校正算法
人工智能·算法·智能手机·计算摄影
WBluuue21 小时前
数据结构与算法:有序表(二):跳表
数据结构·c++·算法·skiplist
x138702859571 天前
c语言中srtlen(指针使用计算字符长度)、传值和传址调用
c语言·开发语言·算法·visual studio
海兰1 天前
【实用程序】电商销售分析仪表盘 — 从零搭建一个AI参与的全栈数据洞察系统
人工智能·学习·算法
zwenqiyu1 天前
P5283 [十二省联考 2019] 异或粽子题解
c++·学习·算法
wayz111 天前
Momentum:TSI(真实强度指数)技术指标详解
算法·金融·数据分析·量化交易·特征工程