第十一届蓝桥杯大赛软件类省赛第二场 H: 子串分值和

在这里插入图片描述
在这里插入图片描述

题解

枚举每个左端点(l), 每个字母独立计算贡献,枚举每个字母,在【l, n】 中找到该字母第一次出现的位置p, 则左端点为 l, 右短点在【p, n】的子串都得到该字母的贡献,贡献 n - p + 1。
例如 :

  • ccaba
  • 当 l = 1 时 :
  • 枚举 ‘a’ , 第一次出现的位置 p = 3 贡献 5 - 3 + 1 = 3。
  • 枚举 ‘b’ …
  • 当 l = 2 …

序列自动机维护下一个字符位置, 复杂度 O(26 * n)

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int maxn = 2e5 + 10, mod = 1e9 + 7, inf = 0x3f3f3f3f;

char s[maxn]; int n, nxt[maxn][30];
void init()
{
    
    
    for(int i = n; i >= 1; i--)
    {
    
    
        for(int j = 1; j <= 26; j++)
            nxt[i-1][j] = nxt[i][j];
        nxt[i-1][s[i]-'a'+1] = i;
    }
}

int main()
{
    
    
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> s + 1;
    n = strlen(s + 1);
    init();
    ll ans = 0;
    for(int i = 1; i <= n; i++)
    {
    
    
        for(int j = 1; j <= 26; j++)
        {
    
    
            int p = nxt[i-1][j];
            if(p == 0) continue;
            ans += (n - p + 1);
        }
    }
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ChinaDevelop/article/details/109137802
今日推荐