当前位置: 首页 > news >正文

ACM训练问题实际代码操作

1.队列与栈的实际应用。

对于每一个样例,第一行是N和一个字符串“FIFO”或者“FILO”(FIFO表示先进先出,即队列;FILO表示先进后出,即栈,),N表示命令的个数,下面有N行,每一行表示一个命令。命令分为两种,IN a 表示进去一个a,OUT表示出一个队头元素或者栈顶元素。

输入格式

第一行是一个数字T,表示样例的个数,对于每一个样例,如题目所述。

输出格式

对于每一个OUT命令,你要根据“FIFO”和“FILO”单独一行输出一个数字,或者输出None如果没有整数了。

#include<vector>
#include <iostream>
#include<string>
#include <queue>
#include <stack>
using namespace std;
int main() {
int T,n;
string S;
cin >> T;

while (T--) {
cin >> n;
cin >> S;
if (S == "FIFO") {
queue<int>x;
for (int i = 1; i <= n; i++) {
string order;
cin >> order;
if (order == "IN") {
int number;
cin >> number;
x.push(number);

}
else {
if (!x.empty()) {
cout << x.front() << endl;
x.pop();
}
else cout << "None"<<endl;
}
}
}
if(S=="FILO") {
stack<int>y;
for (int i = 1; i <= n; i++) {
string order;
cin >> order;
if (order == "IN") {
int number;
cin >> number;
y.push(number);

}
else {
if (!y.empty()) {
cout << y.top() << endl;
y.pop();
}
else cout << "None"<<endl;
}
}
}
}
return 0;
}

2.哈希表的运用。

题目描述

对于N(N≤106)NN≤106)个正整数构成的数组一个一个,请求出数组元素区间和等于mm的区间个数。

输入格式

第一行为两个正整数n,m(1≤n≤106,1≤m≤109) n,m(1≤≤106,1≤M≤109)(nn为数组元素个数,mm为给定值);

第二行为nn个正整数,为数组A的元素值(1≤一个我≤103)(1≤一个​≤103);

输出格式

一个正整数,表示数组一个一个中数据元素的区间和为mm的个数。

#include<vector>
#include <iostream>
#include<string>
#include <queue>
#include <stack>
#include<algorithm>
#include <cstring>
#include <unordered_map>
using namespace std;
long long a[1000005];
int main(){
int n;
long long m;
cin >> n >> m;
unordered_map<long long, int>outcome;
outcome[0] = 1;
vector<long long>pre(n+1,0);
long long count = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pre[i] = a[i] + pre[i - 1];
long long goal = pre[i] - m;
if (outcome.count(goal)) {
count += outcome[goal];
}
outcome[pre[i]]++;
}
cout << count;
return 0;
}


3.图的建立/BFS/DFS

按照输入要求创建图,并分别用深度优先和广度优先两种方法遍历这张图。

输入格式

第一行包含两个整数 n,mn,m (1≤n≤80, n−1≤m≤1500)(1≤n≤80, n−1≤m≤1500),分别表示图中点与边的数量。

其后 mm 行,每行包含两个整数 a,ba,b (0≤a,b<n)(0≤a,b<n),表示 a,ba,b 这两个点之间是连通的。

输出格式

第一行输出按照深度优先搜索方法遍历的结果,第二行输出按照广度优先搜索方法遍历的结果(该题默认根据节点编号从小到大遍历该图)。

#include<vector>
#include <iostream>
#include<string>
#include <queue>
#include <stack>
#include<algorithm>
#include <cstring>
using namespace std;
vector<int> e[100];
vector<int> BFS, DFS;
bool vis[100];
void dfs(int x) {
vis[x] = true;
DFS.push_back(x);
for (int y : e[x])
{
if (!vis[y])
dfs(y);
}
}
void bfs(int x)
{
queue<int> q;
q.push(x);
vis[x] = true;
BFS.push_back(x);
while (q.size()){
int x= q.front();
q.pop();
for (int y : e[x]){
if (!vis[y])
{
q.push(y);
vis[y] = true;
BFS.push_back(y);
}

}
}
}


int main() {
int n, m;//点,边;
cin >> n >> m;

for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
for (int i = 0; i < n; i++)sort(e[i].begin(), e[i].end());

for (int i = 0; i < n; i++)
{
if (!vis[i])
{
dfs(i);
}
}
memset(vis, 0, sizeof vis);
for (int i = 0; i < n; i++) {
if (!vis[i])
{
bfs(i);
}
}
for (int a : DFS) {
cout << a << ' ';
}
cout << endl;
for (int a : BFS) {
cout << a << ' ';
}
cout << endl;


return 0;
}

4.字符串字母前缀统计

#include<vector>
#include <iostream>
#include<string>
#include <queue>
#include <stack>
#include<algorithm>
#include <cstring>
#include <unordered_map>
using namespace std;
int main() {
int n, k, t;
cin >> t;
while (t--) {
cin >> n >> k;
string str1;
string str2;
cin >> str1;
cin >> str2;
int l, r;
vector<vector<int>>pre1(n+1, vector<int>(26,0));
vector<vector<int>>pre2(n + 1, vector<int>(26, 0));
for(int i=0;i<n;i++){
pre1[i+1] = pre1[i];
pre2[i+1] = pre2[i];
pre1[i+1][ str1[i] - 'a']++;
pre2[i+1][ str2[i] - 'a']++;
}
while (k--) {
int count = 0;
cin >> l >> r;

for (int i = 0; i < 26; i++) {
int x1 = pre1[r][i] - pre1[l - 1][i];
int x2 = pre2[r][i] - pre2[l - 1][i];
if (x1 > x2)count += x1 - x2;
}
cout << count << endl;
}
}

5.树的直径

#include <vector> #include <iostream> #include <string> #include <queue> #include <stack> #include <algorithm> #include <cstring> using namespace std; const int N = 100000 + 10; // 定义最大节点数,+10 作为缓冲区 int n, c; // n: 节点数, c: 存储最远节点的临时变量 int count = 0; // 未使用的变量,可忽略 vector<int> e[N]; // 邻接表,e[u] 存储节点 u 的所有邻居 int DFS[N]; // 深度数组,DFS[u] 表示从起点到节点 u 的深度 // DFS 函数:递归遍历树,更新深度并记录最远节点 // u: 当前节点, fa: 父节点(避免回退) void dfs(int u, int fa) { for (int v : e[u]) { // 遍历 u 的所有邻居 v if (v == fa) continue; // 跳过父节点,防止循环 DFS[v] = DFS[u] + 1; // 更新节点 v 的深度(当前深度 +1) if (DFS[v] > DFS[c]) c = v; // 如果 v 的深度大于当前记录的最深节点,更新 c dfs(v, u); // 递归遍历子节点 v } } int main() { cin >> n; // 输入节点数 n // 构建树的邻接表:输入 n-1 条边 for (int i = 1; i <= n-1; i++) { int u, v; cin >> u >> v; // 输入一条边的两个端点 e[u].push_back(v); // 将 v 添加到 u 的邻居列表 e[v].push_back(u); // 将 u 添加到 v 的邻居列表(无向图) } // 第一次 DFS:从节点 1 开始,找到最远节点 c DFS[1] = 0; // 初始化起点深度为 0 c = 1; // 初始化 c 为起点(节点 1) dfs(1, 0); // 调用 DFS,fa=0 表示根节点无父节点 // 第二次 DFS:从第一次找到的最远节点 c 开始,找到直径的另一端点 DFS[c] = 0; // 重置 c 的深度为 0(作为新起点) dfs(c, 0); // 再次调用 DFS,此时 DFS[c] 将存储最大深度(即直径) printf("%d\n", DFS[c]); // 输出树的直径(DFS[c] 的值) return 0; }
http://www.jsqmd.com/news/764363/

相关文章:

  • MCP 2026容器化国产部署失效真相(OpenEuler 22.03 LTS + iSulad + 国产K8s发行版适配断点图谱)
  • 2026年200G光模块品牌推荐:主流厂商测评与高性价比选型指南 - 博客湾
  • SCMP证书多久拿到手? - 众智商学院官方
  • 音乐格式壁垒终结者:Unlock-Music让你的数字音乐真正属于你
  • 推来客网络:扎根成都,打造小程序开发 + 软件定制开发标杆服务商 - 资讯焦点
  • Silk v3音频解码器:轻松解决微信QQ语音格式不兼容问题
  • 首驱S300还值得买吗?适合谁、该不该等、哪些参数需要确认 - 博客万
  • 使用 TaoToken CLI 工具一键为团队统一开发环境配置模型密钥
  • LeagueAkari:如何用本地化智能工具提升你的英雄联盟游戏体验?
  • 现代全栈开发环境搭建:Next.js + Supabase + Resend + Stripe 实战指南
  • 动态上下文记忆管理:突破LLM对话限制的工程实践
  • Unity Prefab进阶玩法:用Prefab Variant和Nested Prefab管理你的复杂游戏场景
  • 2026年4月国内靠谱的梯控系统源头厂家口碑推荐,温感探测器/4G烟雾报警器/智慧楼宇梯控系统,梯控系统供应厂家哪家靠谱 - 品牌推荐师
  • 回森客服人工咨询AI流量赋能,重塑智能科技高效与便捷体验新标杆 - 资讯焦点
  • 上海泽固新型建材:静安抢修料批发选哪家 - LYL仔仔
  • Python子进程管理避坑指南:wait()会卡死?terminate()不灵?一次讲清Popen的正确关闭姿势
  • JenkinsExploit-GUI从下载到打包:避坑指南与自定义Payload集成教程
  • 五一随感
  • 2026年AI模型API中转系统年度测评:五大平台硬核数据对比,为开发者提供权威选型指南
  • 换新手机前必看:保姆级微信数据迁移避坑指南(防中断、防失败、防丢失)
  • 为虚拟机内部署的代码助手配置Claude Code接入Taotoken
  • 从股票分析到日志监控:Pandas时间序列的4个高频实战场景(含resample/shift/rolling详解)
  • 从零部署到SLO达标:MCP 2026推理引擎集成避坑清单(含12个已验证的Kubernetes Operator配置缺陷)
  • CCAA证书有效期多久?需要再注册吗 - 众智商学院官方
  • 别再死记硬背了!Electron IPC通信(ipcRenderer.invoke/send/sendSync)保姆级对比与场景选择指南
  • 2026全光谱健康照明TOP4榜单,雷士凭什么登顶? - 资讯焦点
  • 《QGIS快速入门与应用基础》321:成果验证(如输出指定地图、解决特定问题)
  • JSXBIN解码神器:3分钟掌握Adobe脚本逆向分析核心技术
  • 新手避坑指南:用STM32CubeMX和HAL库点亮LCD1602液晶屏(附4线驱动配置)
  • 时序图vs泳道图