跳到正文

个人记录

题解合集2

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

文章信息

收录
2025年12月12日
更新
2026年3月10日
篇幅
约 11 分钟

版本

  1. v1当前

T1

思路

这道题要求找出最长的词链,其中每个单词都是后一个单词的前缀。所有我们可以利用 字典树 来高效解决这个问题。

在字典树的添加过程中,可以同时维护每个节点的最长词链长度。然后对于每个插入的单词,就可以沿着它的字符路径遍历字典树,在遍历过程中记录当前路径上的最大词链长度。到达单词末尾时,更新该节点的词链长度,并维护全局最大值。

代码

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

using namespace std;
using ll = long long;

#define int ll

const int N = 5e5 + 10;

int n, q, ans;

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

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

T2

思路

这道题要求统计每条暗号能匹配多少条信息,且匹配的两者有相同的前缀。所有我们可以使用 字典树 来高效解决这个问题。

字典树的每个节点维护两个东西:

  • sum[x]sum[x]:经过该节点的信息数量

  • sum2[x]sum2[x]:以该节点结尾的信息数量

查询时,沿着暗号路径遍历字典树,累加沿途的 sum2sum2,最后加上终点的 sumsum。

代码

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

using namespace std;

using ll = long long;

const int N = 5e5 + 10;

int m, n, k, sum[N], sum2[N], ch[N][30], tot = 1;

bool a[N];

void UP(bool p[]){
	int x = 1, tmp = 0;
	for(int i = 1; i <= k; i++){
		if(ch[x][p[i]] == -1){
			ch[x][p[i]] = ++tot;
		}
		x = ch[x][p[i]];
		sum[x]++;
	}
	sum2[x]++;
}
int Query(bool p[]){
	int x = 1, tmp = 0;
	for(int i = 1; i <= k; i++){
		if(ch[x][p[i]] == -1){
			return tmp;
		}
		x = ch[x][p[i]];
		tmp += sum2[x];
	}
	return tmp - sum2[x] + sum[x];
}

int main(){
  	ios::sync_with_stdio(false),cin.tie(0);
    cin >> m >> n;
	memset(ch, -1, sizeof(ch));
    for(int i = 1; i <= m; i++){
    	cin >> k;
    	for(int j = 1; j <= k; j++){
    		cin >> a[j];
		}
		UP(a);
	}
	for(int i = 1; i <= n; i++){
    	cin >> k;
    	for(int j = 1; j <= k; j++){
    		cin >> a[j];
		}
		cout << Query(a) << '\n';
	}
    return 0;
}

T3

思路

这道题要求统计 DNADNA 序列中所有长度为k的子串的出现频率,找出出现次数最多的子串的出现次数。

很明显可以用 HashHash 来写,但是直接 HashHash 应该会 TLETLE 所以可以滚动优化。

代码

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

using namespace std;

using ll = long long;

const int N = 5e5 + 10, B = 911382629, M = 1e9 + 7;

int k, ans = 1;
ll tmp = 0, p = 1;

string s;

unordered_map<ll, int> mp;

int main(){
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> s >> k;
    if(s.size() < k){
        cout << 0;
        return 0;
    }
    for(int i = 0; i < k; i++){
        tmp = (tmp * B + s[i]) % M;
        if(i < k - 1){
            p = (p * B) % M;
        }
    }
    mp[tmp]++;
    for(int i = k; i < s.size(); i++){
        tmp = (tmp - s[i - k] * p % M + M) % M;
        tmp = (tmp * B + s[i]) % M;
        mp[tmp]++;
        ans = max(ans, mp[tmp]);
    }
    cout << ans;
    return 0;
}

T4

思路

直接 HashHash 优化二分即可。

代码

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

using namespace std;

using ll = long long;

const int N = 7e2 + 10, B = 1331, M = 1e9 + 7;

int n, m;

string a[N], b[N];

map<string, int> mp;

bool P(int x, int p){
	for(int i = 1; i <= n; i++){
		mp[a[i].substr(x, p)] = 1;
	}
	for(int i = 1; i <= n; i++){
		if(mp[b[i].substr(x, p)]){
			return 0;
		}
	}
	return 1;
}

int main(){
  	ios::sync_with_stdio(false),cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
    	cin >> a[i];
	}
	for(int i = 1; i <= n; i++){
		cin >> b[i];
	}
	int l = 1, r = m;
	while(l < r){
		int mid = (l + r) / 2;
		int f = 0;
		for(int i = 0; i <= m - mid; i++){
			mp.clear();
			if(P(i, mid)){
				f = 1;
				break;
			}
		}
		if(f){
			r = mid;
		}else{
			l = mid + 1;
		}
	}
	cout << l;
    return 0;
}

T6

思路

可以发现:每次回路操作相当于在图中选择一个环,将环上所有边的颜色翻转。因此,问题转化为能否通过选择若干环的组合,使得所有黑边都被翻转奇数次,白边被翻转偶数次。然后对于每个连通分量,我们只需要考虑其内部的边。不同连通分量之间互不影响。

所以我们可以用并查集,来维护图的连通性

代码

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

using namespace std;
using ll = long long;

#define int ll

const int N = 1e6 + 7;
int n, m, fa[N], dis[N], siz[N], x[N], y[N], kl[N], sum;

int Find(int x) {
    return x == fa[x] ? x : fa[x] = Find(fa[x]);
}

void First(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x != y) fa[x] = y;
}

void Solve(int x, int y) {
    sum -= (dis[x] ? 1 : -1);
    dis[x] = !dis[x];
    sum -= (dis[y] ? 1 : -1);
    dis[y] = !dis[y];
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
    iota(fa + 1, fa + n + 1, 1);
    for (int i = 1; i <= m; i++) {
        cin >> x[i] >> y[i] >> kl[i];
        if (kl[i] == 1) {
            Solve(x[i], y[i]);
        }
        First(x[i], y[i]);
    }
    int cnt = 0;
    for (int i = 1; i <= m; i++) {
        if (kl[i]) {
            int tmp = Find(x[i]);
            cnt += (!siz[tmp]);
            siz[tmp]++;
        }
    }
    int q;
    cin >> q;
    while (q--) {
        int op;
        cin >> op;
        if (op == 2) {
            if (sum) cout << -1 << "\n";
            else cout << cnt << "\n";
        } else {
            int p;
            cin >> p;
            p++;
            Solve(x[p], y[p]);
            int tmp = Find(x[p]);
            if (kl[p]) {
                kl[p] = 0;
                siz[tmp]--;
                cnt -= !(siz[tmp]);
            } else {
                kl[p] = 1;
                cnt += !(siz[tmp]);
                siz[tmp]++;
            }
        }
    }
    return 0;
}
原文