跳到正文

个人记录

tj2

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

文章信息

发布
2025年10月24日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

T1 01 串 (binary)

思路

根据 n,mn, m 的奇偶性模拟即可。

代码

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

using namespace std;
using ll = long long;

const int N = 1e5 + 10;

int n, m;

void Solve(){
	cin >> n >> m;
	if(n == 1 || (m <= 1 && (n % 2))){
		cout << "-1\n-1" << '\n';
	}else if(n & 1){
		for(int i = 1; i <= n - 2; i++)cout << 1;
		cout << "0101";
		for(int i = 1; i <= m - 2; i++)cout << 0;
		cout << '\n';
		if(m & 1){
			cout << 10;
			for(int i = 1; i <= m - 3; i++)cout << 0;
			cout << "1010";
			for(int i = 1; i <= n - 3; i++)cout << 1;
		}else{
			cout << 1;
			for(int i = 1; i <= m - 2; i++)cout << 0;
			cout << "0101";
			for(int i = 1; i <= n - 3; i++)cout << 1;
		}
		cout << '\n';
	}else{
		for(int i = 1; i <= n; i++)cout << 1;
		for(int i = 1; i <= m; i++)cout << 0;
		cout << '\n';
		if(m & 1){
			cout << 1;
			for(int i = 1; i <= m - 1; i++)cout << 0;
			cout << 10;
			for(int i = 1; i <= n - 2; i++)cout << 1;
		}else{
			cout << 1;
			for(int i = 1; i <= m; i++)cout << 0;
			for(int i = 1; i < n; i++)cout << 1;
		}
		cout << '\n';
	}
}

int main(){
	freopen("binary.in", "r", stdin);
	freopen("binary.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0);
	int T;
	cin >> T;
	while(T--){
		Solve();
	}
	return 0;
}

T2 树 (tree)

思路

可以发现题面了讲了,可以走到边上,所以问题就变成了,找到 u,vu, v 简单最短路,然后算出要走多少步。

所以直接 LCALCA 即可。

代码

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

using namespace std;
using ll = long long;

const int N = 1e4 + 10;

struct Node{
	int v, w;
};

vector<Node> v[N];

int o[N], f[N][30], ans[N], n, m;

void dfs(int x, int fa){
	o[x] = o[fa] + 1;
 	f[x][0] = fa;
 	for(int i = 1; i < 30; i++){
 		f[x][i] = f[f[x][i - 1]][i - 1];
	}
	for(Node i : v[x]){
		if(i.v == fa)continue;
		ans[i.v] = ans[x] + i.w;
		dfs(i.v, x);
	}
}

int Query(int x, int y){
	if(o[x] < o[y])swap(x, y);
	int tmp = 30 - 1;
	while(o[x] > o[y] && ~tmp){
		if(o[f[x][tmp]] >= o[y]){
			x = f[x][tmp];
		}
		tmp--;
	}
	tmp = 30 - 1;
	while(x != y && ~tmp){
		if(f[x][tmp] != f[y][tmp]){
			x = f[x][tmp];
			y = f[y][tmp];
		}
		tmp--;
	}
	if(x != y){
		x = f[x][0];
		y = f[y][0];
	}
	return x;
}

int main(){
	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for(int i = 1, x, y, w; i < n; i++){
		cin >> x >> y >> w;
		v[x].push_back({y, w});
		v[y].push_back({x, w});
	}
	dfs(1, 0);
	cin >> m;
	for(int i = 1, a, b, c; i <= m; i++){
		cin >> a >> b >> c;
		int lca = Query(a, b);
		int tmp = ans[a] + ans[b] - ans[lca] * 2;
		cout << ceil(tmp * 1.0 / c) << '\n';
	}
	return 0;
}

T3 括号序列(bracket)

思路

可以观察到

  • 如果一个合法序列包含另一个合法序列,那么外层序列的所有位置都包含内层序列
  • 合法序列可以向左右扩展

那么我们可以先使用栈找到所有匹配的括号对

然后用 DPDP 计算每个位置能向左扩展的合法序列数和计算每个位置能向右扩展的合法序列数。

最后的答案即为结合扩展信息和嵌套关系计算每个位置被包含的次数

代码

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

using namespace std;
using ll = long long;

#define int ll

const int N = 1e7 + 10, MOD = 1e9 + 7;

string s;

int mp[N], a[N], b[N], sk[N], top, op[N], ans[N];

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> s;
  int n = s.size();
  s = '#' + s;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(') {
      op[i] = sk[top];
      sk[++top] = i;
    } else if (top) {
      mp[sk[top]] = i;
      mp[i] = sk[top];
      top--;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!mp[i] || s[i] == '(') continue;
    b[i] = b[mp[i] - 1] + 1;
  }
  for (int i = n; i >= 1; i--) {
    if (!mp[i] || s[i] == ')') continue;
    a[i] = a[mp[i] + 1] + 1;
  }
  for (int i = 1; i <= n; i++) {
    if (!mp[i] || s[i] == ')') continue;
    ans[i] = a[i] * b[mp[i]] % MOD;
    if (op[i]) (ans[i] += ans[op[i]]) %= MOD;
    ans[mp[i]] = ans[i];
  }
  int res = 0;
  for (int i = 1; i <= n; i++) { res += i * ans[i] % MOD; }
  cout << res;
  return 0;
}
原文