求分析时间复杂度(CSP-S T2)
  • 板块灌水区
  • 楼主unsigned_char
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/22 13:55
  • 上次更新2023/11/2 11:55:11
查看原帖
求分析时间复杂度(CSP-S T2)
421773
unsigned_char楼主2023/10/22 13:55
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstdio>
using namespace std;

struct Node
{
    int l, r;
    Node(int _l, int _r)
    {
        l = _l;
        r = _r;
    }
};

bool operator<(const Node &a, const Node &b)
{
    if(a.l != b.l)
        return a.l < b.l;
    return a.r < b.r;
}

bool operator==(const Node &a, const Node &b)
{
    return a.l == b.l && a.r == b.r;
}

int main()
{
    freopen("game.in", "r", stdin);
    freopen("game.out", "w", stdout);
    int n;
    string s;
    cin >> n >> s;
    long long ans = 0;
    queue<Node> tmp;
    set<Node> a, b;
    map<Node, bool> flag;
    for(int i = 0; i < s.length() - 1; ++i)
        if(s[i] == s[i + 1])
            tmp.push(Node(i, i + 1));
    while(!tmp.empty())
    {
        a.clear();
        while(!tmp.empty())
        {
            Node t = tmp.front();
            tmp.pop();
            flag[t] = true;
            a.insert(t);
            b.insert(t);
            ++ans;
        }
        for(auto i : a)
            if(i.l > 0 && i.r < s.length() - 1)
                if(s[i.l - 1] == s[i.r + 1])
                {
                    if(flag[Node(i.l - 1, i.r + 1)])
                        continue;;
                    flag[Node(i.l - 1, i.r + 1)] = true;
                    tmp.push(Node(i.l - 1, i.r + 1));
                }
        for(auto i : b)
            for(auto j = b.upper_bound(Node(i.r + 1, 0)); j != b.end() && (*j).l == i.r + 1; ++j)
            {
                if(flag[Node(i.l, (*j).r)])
                    continue;
                flag[Node(i.l, (*j).r)] = true;
                tmp.push(Node(i.l, (*j).r));
            }

    }
    cout << ans;
    fclose(stdin);
    fclose(stdout);
    return 0;
}

CSP-S T2,洛谷自测subtask #0 60pts,subtask #1 50pts

2023/10/22 13:55
加载中...