字典树模板+位运算

news/2024/7/7 19:56:45 标签: c++, 算法

P3879 [TJOI2010] 阅读理解 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

trie树板子题,稍微有一丢丢不一样,套用字典树模板稍加修改就能过

手搓字典树代码:

char ch[1010][26], cnt[1010], idx;
void insert(string s)//插入
{
	int p = 0;
	for (int i = 1; s[i]; i++)
	{
		int j = s[i] - 'a';
		if (!ch[p][j])
		{
			ch[p][j] = ++idx;
		}
		p = ch[p][j];
	}
	cnt[p]++;
}
int query(string s)//查询
{
	int p = 0;
	for (int i = 1; s[i]; i++)
	{
		int j = s[i] - 'a';
		if (!ch[p][j])
		{
			return 0;
		}
		p = ch[p][j];
	}
	return cnt[p];
}

题解代码:

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
using namespace std;
typedef long long ll;
char s[50010];
char ch[500010][26], idx;
char w[500010][1010];
int n, m;
inline int read()
{
	int k = 0, f = 1; 
	char ch = getchar();
	while (ch < '0' || ch>'9')
	{
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		k = k * 10 + ch - '0';
		ch = getchar();
	}
	return k * f;
}
void insert(int x)
{
	scanf("%s", s + 1);
	int l = strlen(s + 1);
	int p = 0;
	for (int i = 1; i<=l; i++)
	{
		int j = s[i] - 'a';
		if (!ch[p][j])
		{
			ch[p][j] = idx++;
		}
		p = ch[p][j];
	}
	w[p][x] = 1;
}
void check()
{
	scanf("%s", s + 1);
	int l = strlen(s + 1);
	int p = 0;
	int flag = 1;
	for (int i = 1; i<=l; i++)
	{
		int j = s[i] - 'a';
		if (!ch[p][j])
		{
			flag = 0;
			break;
		}
		p = ch[p][j];
	}
	if (flag)
	{
		for (int i = 1; i <= n; i++)
		{
			if (w[p][i])
			{
				cout << i << " ";
			}
		}
		cout << endl;
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	n = read();
	for (int i = 1; i <= n; i++)
	{
		int x = read();
		for (int j = 1; j <= x; j++)
		{
			insert(i);
		}
	}
	m = read();
	for (int i = 1; i <= m; i++)
	{
		check();
	}
	return 0;
}

位运算:

左移:

1<<n=2^n,n<<1=2n

右移:

n>>1=n/2

取出n在二进制下的第k位:(n>>k)&1

取出n在二进制下的第0~k-1位(后k位):n&((1<<k)-1)

把n在二进制下的第k位取反:n xor(1<<k)

对n在二进制下的第k位赋值1:n|(1<<k)

对n在二进制下的第k位赋值0:n&(~(1<<k))

来两道位运算的题目练练手(算法竞赛进阶指南)

P1226 【模板】快速幂 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

思路:

解决这道题有两种方法:

1.分治思想:n为偶数时把2^n分成a^(n/2),n为奇数时分为a^(n/2)*a

2.倍增思想:稍微有一点难理解(

从头开始。若当前 p 为偶数,咱们不着急,只需把 x 自乘,然后 p/=2 (即考虑下一层,下几层会帮我们乘上 (x2)p/2的)。

若当前 p 为奇数,说明 p=x∗(x2)(p−1)/2 中前面那个 x 的存在,ans∗=x。然后继续考虑下一层(下几层会帮我们乘上(x2)(p−1)/2的)

)

代码1(分治):
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
inline int read()
{
	int k = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch>'9')
	{
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		k = k * 10 + ch - '0';
		ch = getchar();
	}
	return k * f;
}
int solve(ll a, ll b, ll p)
{
	if (b == 0)
		return 1;
	ll ans = solve(a, b / 2, p) % p;
	if (b % 2 == 0)
	{
		return ans * ans % p;
	}
	else
	{
		return ans * ans % p * a % p;
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	ll a, b, p;
	cin >> a >> b >> p;
	cout << a << "^" << b << " mod " << p << "=" << solve(a, b, p) << endl;
	return 0;
}
代码2(倍增):
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
inline int read()
{
	int k = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch>'9')
	{
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		k = k * 10 + ch - '0';
		ch = getchar();
	}
	return k * f;
}
void solve(ll x, ll y, ll p)
{
	ll ans = 1 % p;
	for (; y; y >>= 1)
	{
		if (y & 1)
		{
			ans = ans * x % p;
		}
		x = x * x % p;
	}
	cout << x << "^" << y << " mod " << p << "=" << ans << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	ll a, b, p;
	cin >> a >> b >> p;
	solve(a, b, p);
	return 0;
}

P10446 64位整数乘法 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

代码:

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
inline int read()
{
	int k = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch>'9')
	{
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		k = k * 10 + ch - '0';
		ch = getchar();
	}
	return k * f;
}
void solve(ll x, ll y, ll z)
{
	ll ans = 0;
	for (; y; y >>= 1)
	{
		if (y & 1)
			ans = (ans + x) % 5;
		x = (x * 2) % z;
	}
	cout << ans;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	ll a, b, p;
	cin >> a >> b >> p;
	solve(a, b, p);
	return 0;
}


http://www.niftyadmin.cn/n/5535013.html

相关文章

FastGPT 报错:undefined 该令牌无权使用模型:gpt-3.5-turbo (request id: xxx)

目录 一、FastGPT 报错 二、解决方法 一、FastGPT 报错 进行对话时 FastGPT 报错如下所示。 [Error] 2024-07-01 09:25:23 sse error: undefined 该令牌无权使用模型:gpt-3.5-turbo (request id: xxxxx) {message: 403 该令牌无权使用模型:gpt-3.5-turbo (request id: x…

Java WebService记

Web Services开发 常用的 Web Services 框架有 Apache Axis1 、 Apache Axis2 、 Apache CXF &#xff0c;而 Apache Axis1 已经逐渐被淘汰所以本文不会讨论&#xff0c;重点关注 Apache Axis2 及 Apache CXF 。 Apache Axis2 在IDEA中新建 Axis2Demo 项目后右键选择 添加框架…

QT_GUI

1、QT安装 一个跨平台的应用程序和用户界面框架&#xff0c;用于开发图形用户界面&#xff08;GUI&#xff09;应用程序以及命令行工具。QT有商业版额免费开源版&#xff0c;一般使用免费开源版即可&#xff0c;下面安装的是QT5&#xff0c;因为出来较早&#xff0c;使用较多&…

1.英语中的从句学习

名词性从句&#xff1a; 1.最常见的连接词是that在宾语从句中的运用&#xff0c;如&#xff1a;I know that you will come. 句中的that 就是连接词&#xff0c;作用就是连接主句和从句&#xff0c;不充当成分也没有含义&#xff0c;只起风向标的作用&#xff0c;告诉你接下来…

Golang 开发实战day15 - Input info

&#x1f3c6;个人专栏 &#x1f93a; leetcode &#x1f9d7; Leetcode Prime &#x1f3c7; Golang20天教程 &#x1f6b4;‍♂️ Java问题收集园地 &#x1f334; 成长感悟 欢迎大家观看&#xff0c;不执着于追求顶峰&#xff0c;只享受探索过程 Golang 开发实战day15 - 用户…

JVM专题之Java对象内存模型

一个Java对象在内存中包括3个部分: 对象头、实例数据和对齐填充 数据 内存 -- CPU 寄存器 -127 补码 10000001 - 11111111 32位的处理器 一次能够去处理32个二进制位 4字节的数据 64位操作系统 8字节 2的64次方的寻址空间 指针压缩…

Vue3 Hooks 用法 scrollTop, mousemoveHandler,useCountDown

三个实例来自 learn_vue: 【教学工程】学习vue2/vue3 (gitee.com) 目录 1. 何为Hooks 2. 使用场景 3. 常见的 Hooks 函数 4. 实例 4.1简易hook 例子 4.2 自定义scrolltop例子 4.3 mousemoveHandler例子 4.4 useCountDown例子 1. 何为Hooks Hooks 是一种函数&#xff0c;用于…

Linux线程:编织并发的梦幻世界

目录 &#x1f6a9;引言 &#x1f6a9;听故事&#xff0c;引概念 &#x1f6a9;生产者消费者模型 &#x1f680;再次理解生产消费模型 &#x1f680;挖掘特点 &#x1f6a9;条件变量 &#x1f680;条件变量常用接口 &#x1f680;条件变量的原理 &#x1f6a9;引言 上一篇…