Acwing76场周赛

题目链接

这次还是只做出来两道题,前两题都挺简单的,注意第二题需要开long long不开会wa,代码粘上来,以后可能会看吧


第一题

#include<iostream>
#include <string>
using namespace std;
string a, b;
int main()
{
    char str1[110], str2[110];
    cin >> str1 >> str2;
    a += str1; b += str2;
    if(a.length() != b.length())
    {
        cout << "NO";
        return 0;
    }
    for(int i = 0, j = b.length() - 1; i < a.length(); i ++ ,j --)
    {
        if(a[i] != b[j])
        {
             cout << "NO";
            return 0;
        }
    }
    cout << "YES";
    return 0;
}

第二题


#include <iostream>
#include <unordered_map>
using namespace std;

const int N = 1e5 + 10;
typedef long long ll;
unordered_map<char, ll>mp;
ll ans;
char a[N];

int main()
{
    cin >> a;
    for(int i = 0; a[i]; ++i) mp[a[i]] ++;
    for(auto x : mp)
    {
        ll k = x.second;
        ans += k * k;
    }
    cout << ans << endl;
    return 0;
}

第三题

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1010, M = N * 10;

typedef long long ll;
char str[N];
int d[N], n;
bool inq[N];

int h[N], e[M], w[M], ne[M], idx;
void add(int a, int b, int c)
{
    w[idx] = c; e[idx] = b; ne[idx] = h[a]; h[a] = idx ++;
    // cout << a << ' '<< b << ' ' << c << endl;
}

void spfa()
{
    queue<int>q;
    memset(d, -0x3f, sizeof d);
    d[0] = 0;q.push(0);inq[0] = 1;
    while(q.size())
    {
        int t = q.front();q.pop();inq[t] = 0;
        for(int i = h[t]; i != -1; i =ne[i])
        {
            int j = e[i];
            if(d[j] < d[t] + w[i])
            {
                d[j] = d[t] + w[i];
                if(!inq[j])
                {
                    inq[j] = 1;
                    q.push(j);
                }
            }
        }
    }
}

int main()
{
    memset(h, -1, sizeof h);
    cin >> n >> (str + 1);
    for(int i = 1; i <= n - 1; ++ i)
    {
        if(str[i] == '<')    add(i, i + 1, 1);
        else if(str[i] == '>')  add(i + 1, i, 1);
        else if(str[i] == '=')  add(i, i + 1, 0), add(i + 1, i, 0);
    }
    for(int i = 1; i <= n; ++ i)    add(0, i, 1);
    spfa();
    for(int i = 1; i <= n; ++ i)    cout << d[i] << ' ';
    return 0;
}
posted @ 2022-11-06 10:36  cxy8  阅读(17)  评论(0编辑  收藏  举报