牛客算法周周练17 DE

题目链接:https://ac.nowcoder.com/acm/contest/6607#question

D-刺客信条 

题意:求S到E花的最短时间,a[i][j]表示经过的时间,ABC需要花费100。 

hint:bfs+优先队列搜最短路 (优先队列的两种应用)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int, int>
#define PIII pair<int, PII>
#define FI first
#define SE second
const int inf = 0x3f3f3f3f;
char s[55][55];
int g[55][55], dis[55][55];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int n, m, sx, sy, ex, ey;
struct node
{
    int x, y, cost;
    bool operator<(const node &a) const
    {
        return cost > a.cost;
    }
};
void bfs1()
{
    memset(dis, -1, sizeof(dis));
    priority_queue<node> q;
    dis[sx][sy] = 0;
    q.push({sx, sy, dis[sx][sy]});
    while (q.size())
    {
        node t = q.top();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int x, y;
            x = t.x + dx[i];
            y = t.y + dy[i];
            if (x < 1 || x > n || y < 1 || y > m || dis[x][y] != -1)
                continue;
            dis[x][y] = dis[t.x][t.y] + g[x][y];
            q.push({x, y, dis[x][y]});
        }
    }
}
void bfs()
{
    memset(dis, -1, sizeof(dis));
    priority_queue<PIII, vector<PIII>, greater<PIII>> q;
    dis[sx][sy] = 0;
    q.push({dis[sx][sy], {sx, sy}});
    while (q.size())
    {
        auto t = q.top();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int x, y;
            x = t.SE.FI + dx[i];
            y = t.SE.SE + dy[i];
            if (x < 1 || x > n || y < 1 || y > m || dis[x][y] != -1)
                continue;
            dis[x][y] = dis[t.SE.FI][t.SE.SE] + g[x][y];
            q.push({dis[x][y], {x, y}});
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> s[i][j];
            if (s[i][j] == 'S')
                sx = i, sy = j;
            else if (s[i][j] == 'E')
                ex = i, ey = j;
            else if (s[i][j] >= 'A' && s[i][j] <= 'C')
                g[i][j] = 100;
            else
                g[i][j] = s[i][j] - '0';
        }
    }
    bfs1();
    cout << dis[ex][ey] << endl;
    return 0;
}

 E-我不爱他

 题意:t组数据,n个字符串,求si,sj最大前后缀匹配和(1<=si,sj<=n)

 

hint:hash+kmp 。牛客第二场多校A有类似的题:https://blog.csdn.net/weixin_43911947/article/details/107369844

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int base = 233;
#define ll long long
#define ull unsigned long long
unordered_map<ull, int> cnt;
string s[N];
int nxt[N], now[N];
void get_nxt(string s, int *nxt)
{
    int len = s.size(), i = 0, j = -1;
    nxt[i] = -1;
    while (i < len)
    {
        if (j == -1 || s[i] == s[j])
            nxt[++i] = ++j;
        else
            j = nxt[j];
    }
}
void Hash_pos(string s)
{
    int len = s.size();
    ull ans = 0, p = 1;
    for (int i = len - 1; i >= 0; i--)
    {
        ans = ans + p * (s[i] - 'a' + 1);
        p = p * base;
        cnt[ans]++;
    }
}
void Hash_pre(string s)
{
    int len = s.size();
    ull ans = 0;
    for (int i = 0; i < len; i++)
    {
        ans = ans * base + s[i] - 'a' + 1;
        now[i] = cnt[ans];
    }
}
void init()
{
    memset(now, 0, sizeof(now));
    cnt.clear();
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        init();
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            cin >> s[i];
            Hash_pos(s[i]);
        }
        ull sum = 0;
        for (int i = 1; i <= n; i++)
        {
            int len = s[i].size();
            get_nxt(s[i], nxt);
            Hash_pre(s[i]);
            for (int j = 0; j < len; j++)
                now[nxt[j + 1] - 1] -= now[j];
            for (int j = 0; j < len; j++)
                sum += now[j] * (j + 1);
        }
        cout << sum << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值