AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 3175. 人物相关性分析(前缀和)    原题链接    简单

作者: 作者的头像   静静在Coding ,  2023-01-26 00:00:14 ,  所有人可见 ,  阅读 66


0


#include <bits/stdc++.h>
#include <cstdio>
#include <string>
using namespace std;
const int N = 1000010;
int sa[N];  // 求Alice 出现的坐标标示为1 当一个Bob出现求sa[l ~ r]

int main()
{
    int k;
    string str;
    cin >> k;
    getchar();
    getline(cin, str);
    long long res = 0;

    for (int i = 0; i < str.size(); i++) {
        i = str.find("Alice", i);
        if (i == string::npos)
            break;
        if ((i + 5 < str.size() && isalpha(str[i + 5])) || (i >= 1 && isalpha(str[i - 1]))) {
            i = i + 5;
            continue;
        }
        sa[i + 1] = true;
    }

    for (int i = 1; i <= str.size(); i++) {
        sa[i] += sa[i - 1];
    }

    for (int i = 0; i < str.size(); i++) {
        i = str.find("Bob", i);
        if (i == string::npos)
            break;

        if ((i + 3 < str.size() && isalpha(str[i + 3])) || (i >= 1 && isalpha(str[i - 1]))) {
            i = i + 3;
            continue;
        }
        // A ... Bob ... A  //坐标后面要加1 s[]数组偏移了
        int l = max(0, i - k - 5) + 1, r = min(i + 3 + k + 1, (int)str.size());

        res += sa[r] - sa[l - 1];
    }

    cout << res << endl;
    return 0;
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息