跳到正文

个人记录

题解合集

czhusi约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年7月22日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 9 分钟

版本

  1. v1当前

T1

题意

定义 f(x,y)f(x,y) 为 x,yx,y 的最初公共前缀的长度。

给定 nn 个字符串求:

∑i=1n−1∑j=i+1nf(si,sj)\sum_{i = 1}^{n - 1}\sum_{j = i + 1}^{n}f(s_i, s_j)

思路

最长公共前缀性质:两个字符串的最长公共前缀 == 这两个字符串的公共前缀数量。

于是字典树维护即可。

代码

C++
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

#define int ll

const int N = 3e5 + 10;

int n, ans;

string s;

struct Trie{
  int ch[N][30], tot, cnt[N];
  void Insert(const string &s){
    int x = 0;
    for(char i : s){
      cnt[x]++;
      if(!ch[x][i - 'a']){
        ch[x][i - 'a'] = ++tot;
      }
      x = ch[x][i - 'a'];
      ans += cnt[x];
    }
    cnt[x]++;
  }
}T;

signed main(){
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for(int i = 1; i <= n; i++){
    cin >> s;
    T.Insert(s);
  }
  cout << ans;
  return 0;
}

T2

题意

给出数组 a,ba,b,以及 QQ 次询问,询问 (ali…ari)(a_{l_i}\dotsc a_{r_i}) 与 (bLi…BRi)(b_{L_i}\dotsc B_{R_i}) 经过重排后是否相等。

思路

可以想到,询问的意思是这两个区间内所有数字出现的数量相同。

便可以随机给每个数分配一个 HashHash 值,然后求两遍前缀和即可。

代码

C++
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

#define int ll

const int N = 2e5 + 10;

int n, q, a[N], b[N], sum1[N], sum2[N], mp[N];

mt19937_64 Rand(time(0));

signed main(){
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> q;
  for(int i = 1; i <= n; i++){
    mp[i] = Rand();
  }
  for(int i = 1; i <= n; i++){
    cin >> a[i];
  }
  for(int i = 1; i <= n; i++){
    cin >> b[i];
  }
  for(int i = 1; i <= n; i++){
    sum1[i] = sum1[i - 1] + mp[a[i]];
    sum2[i] = sum2[i - 1] + mp[b[i]];
  }
  for(int i = 1, x, y, x2, y2; i <= q; i++){
    cin >> x >> y >> x2 >> y2;
    cout << (sum1[y] - sum1[x - 1] == sum2[y2] - sum2[x2 - 1] ? "Yes" : "No") << '\n';
  }
  return 0;
}

T3

题意

给定一个长度为 LL 的字符串,你需要找出一个最长的子串,并且使得这个子串在字符串中出现了至少两次(在不同的位置)。输出它的长度。

思路

很好想到二分与单调性,但是直接二分会超时。 所以可以用哈希优化判断不同子串的出现次数的过程。

代码

C++
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 10, M1 = 998244353, M2 = 1e9 + 7, B1 = 233, B2 = 39;

pair<int, int> op[N];

int a[N], n, H1[N], H2[N], P1[N], P2[N];

int Hash1(int l, int r){
    return ((H1[r] - H1[l - 1] * 1ll * P1[r - l + 1]) % M1 + M1) % M1;
}

int Hash2(int l, int r){
    return ((H2[r] - H2[l - 1] * 1ll * P2[r - l + 1]) % M2 + M2) % M2;
}

bool P(int x){
    for (int i = x; i <= n; i++)op[i - x + 1] = {Hash1(i - x + 1, i), Hash2(i - x + 1, i)};
    sort(op + 1, op + n - x + 2);
    for (int i = 2; i <= n - x + 1; i++)if (op[i - 1] == op[i])return 1;
    return 0;
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++){
        char c;
        cin >> c;
        a[i] = c - 'a';
    }
    P1[0] = P2[0] = 1;
    for (int i = 1; i <= n; i++){
        H1[i] = (H1[i - 1] * 1ll * B1 + a[i]) % M1;
        H2[i] = (H2[i - 1] * 1ll * B2 + a[i]) % M2;
        P1[i] = (P1[i - 1] * 1ll * B1) % M1;
        P2[i] = (P2[i - 1] * 1ll * B2) % M2;
    }
    int l = 0, r = n - 1;
    while (r > l){
        int mid = l + r + 1 >> 1;
        if (P(mid))l = mid;
        else r = mid - 1;
    }
    cout << l;
    return 0;
}

T4

题意

给定一个字符串,找出其中最长的 “双倍回文” 子串的长度。如果不存在这样的子串,输出 00。

双倍回文的定义:

  • 子串长度必须是4的倍数。

  • 子串本身是回文。

  • 子串的前半部分(前一半字符)也是回文

思路

为了高效地找到所有回文子串,我们使用 ManacherManacher 算法

然后在 ManacherManacher 算法的扩展过程中,对于每个位置i,如果当前回文长度可以扩展,则检查是否存在满足条件的双倍回文子串。

优化:在扩展过程中,只检查长度是4的倍数的子串,以减少不必要的计算。

代码

C++
#include <bits/stdc++.h>

using namespace std;

const int N = 2e6 + 10;

int p[N], n, ans;

string s;

void Manacher(string &s, int n) {
    int c = 0, r = 0;
    for (int i = 1; i <= n; i++) {
        if (i < r) {
            p[i] = min(p[2 * c - i], r - i);
        } else {
            p[i] = 1;
        }
        while (s[i + p[i]] == s[i - p[i]]) {
            p[i]++;
        }
        if (i + p[i] > r) {
            if (i & 1) {
                for (int j = max(r, i + 4); j < i + p[i]; j++) {
                    if (!((j - i) & 3) && p[i - (j - i) / 2] > (j - i) / 2) {
                        ans = max(ans, j - i);//当找到一个长度是4的倍数的回文子串,并且它的前半部分也是回文
                    }
                }
            }
            r = i + p[i];
            c = i;
        }
    }
}

int main() {
    cin >> n >> s;
    s = ' ' + s;
    s = s + s;
    for (int i = n; i >= 1; i--) {
        s[i * 2 + 1] = '#';
        s[i * 2] = s[i];
    }
    s[1] = '#';
    Manacher(s, 2 * n + 1);
    cout << ans;
    return 0;
}
原文