首页 > 其他分享 >[考试记录] 2024.6.9

[考试记录] 2024.6.9

时间:2024-06-09 14:23:09浏览次数:16  
标签:string 2024.6 记录 int dir else rd 考试 tractor

T3 WRONG - Wrong directions

题面翻译

Farmer John刚刚购买了一台新型可编程拖拉机。为了使拖拉机移动,他输入一串长度为N的绳子

(1 <= N <= 100,000)仅由字符F,L和R组成。每个'F'指示拖拉机向前移动一个单元,

并且字符'L'和'R'分别导致左右转90度。拖拉机从原点开始(0,0)

朝北。

通过输入他想要的命令字符串对他的拖拉机进行编程后,FJ记得他输入了一个字符

命令字符串不正确,但他不记得是哪一个!例如,他可能在打算输入'F'或'L'时

string包含字符'R'。请计算拖拉机可能在飞机上的不同位置的数量

最后结果(拖拉机在最终位置所面向的方向无关紧要)。

题目描述

Farmer John has just purchased a fancy new programmable tractor. To make the tractor move, he types in a string of length N

(1 <= N <= 100,000) consisting of only the characters F, L, and R. Each 'F' instructs the tractor to move forward one unit,

and the characters 'L' and 'R' result in left and right turns of 90 degrees, respectively. The tractor starts out at the origin (0,0)

facing north.

After programming his tractor by typing in his intended command string, FJ remembers that he typed exactly one character in

the command string incorrectly, but he can't remember which one!For example, he might have typed 'F' or 'L' when his intended

string contained the character 'R'. Please compute the number of different locations in the plane at which the tractor might

end up as a result (the direction the tractor faces in its final location does not matter).

Input format: * Line 1: Farmer John's intended command string. SAMPLE INPUT: FF INPUT DETAILS: Farmer John wants the tractor to advance forward twice, ideally ending at position (0,2). OUTPUT FORMAT: * Line 1: The number of positions at which the tractor might end up,given that FJ mistypes one of the characters in his command string. SAMPLE OUTPUT 3 OUTPUT DETAILS: There are 4 possible mistyped sequences: FL, FR, LF, an RF. These will land the tractor at (0,1), (0,1), (-1,0), and (1,0) respectively,  a total of 3 distinct locations.

解析

由于我们只修改其中一个字符,所以问题就会变的简单许多。为了减少运算量,可以先预处理出从开始点到中间某一个点时的 \(x\)、\(y\) 和方向。同样预处理出从中间某一个点到终点的 \(x\)、\(y\) 和方向,在预处理时就把起点作为 \((0,0)\)。

对于修改第 \(k\) 个字符,可以把整段路径看作 \(1~k-1\)、\(k\)、\(k+1~len\) 三部分,第一部分和第三部分都预处理过了,只用合并就行了。方向的改变对应着坐标的变换,手推一下就行了。在合并的同时记录一下坐标,可以用 map,我用的哈希表,能快一点。

氧气有毒建议哈希乘数取131

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int hmod = 999983, hmax = 1e7 + 5, N = 2e5 + 5;
int len;
int way[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int my[4][2] = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
string s;
long long ans;
struct dirt{ int dir, x, y; }ld[N], rd[N];
struct Hash_Table{
	int h[hmod+1], nxt[hmax+1], cnt, vx[hmax+1], vy[hmax+1];
	void Hash_in(int x, int y){
		int hnum = (__int128)(x*131 + y) % hmod;
		for(int i=h[hnum]; i; i=nxt[i])
			if(vx[i] == x && vy[i] == y) return;
		vx[++cnt] = x, vy[cnt] = y, nxt[cnt] = h[hnum], h[hnum] = cnt;
	}
	bool Hash_out(int x, int y){
		int hnum = (__int128)(x*131 + y) % hmod;
		for(int i=h[hnum]; i; i=nxt[i])
			if(vx[i] == x && vy[i] == y) return 1;
		return 0;
	}
}h;
inline void merge(int a, char ch, int b){
	int x = ld[a].x, y = ld[a].y, d = ld[a].dir;
	if(ch == 'F') x += way[d][0], y += way[d][1];
	else d = (d + (ch=='L'?1:-1) + 4) % 4;
	if(d == 0) x += rd[b].x, y += rd[b].y;
	else if(d == 1) x -= rd[b].y, y += rd[b].x;
	else if(d == 2) x -= rd[b].x, y -= rd[b].y;
	else x += rd[b].y, y -= rd[b].x;
	if(!h.Hash_out(x, y)) ++ans, h.Hash_in(x, y);
}
int main(){
	freopen("wrongdir.in", "r", stdin);
	freopen("wrongdir.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin>>s; len = s.size();
	for(int i=1; i<=len; ++i){
		if(s[i-1] == 'F'){
			ld[i].dir = ld[i-1].dir;
			ld[i].x = ld[i-1].x + way[ld[i].dir][0];
			ld[i].y = ld[i-1].y + way[ld[i].dir][1];
		} else{
			ld[i].dir = (ld[i-1].dir + (s[i-1]=='L'?1:-1) + 4) % 4;
			ld[i].x = ld[i-1].x, ld[i].y = ld[i-1].y;
		}
	}
	for(int i=len; i>0; --i){
		if(s[i-1] == 'F'){
			rd[i].dir = rd[i+1].dir;
			rd[i].x = rd[i+1].x;
			rd[i].y = rd[i+1].y + 1;
		} else{
			rd[i].x = rd[i+1].y;
			rd[i].y = rd[i+1].x * -1;
			if(s[i-1] == 'R') rd[i].dir = (rd[i-1].dir + 3) % 4;
			else{
				rd[i].x *= -1, rd[i].y *= -1;
				rd[i].dir = (rd[i-1].dir + 1) % 4;
			}
		}
	}
	for(int i=1; i<=len; ++i){
		if(s[i-1] == 'F') merge(i-1, 'L', i+1), merge(i-1, 'R', i+1);
		else if(s[i-1] == 'R') merge(i-1, 'F', i+1), merge(i-1, 'L', i+1);
		else merge(i-1, 'F', i+1), merge(i-1, 'R', i+1);
	} return cout<<ans, 0;
}

标签:string,2024.6,记录,int,dir,else,rd,考试,tractor
From: https://www.cnblogs.com/xiaolemc/p/18239532

相关文章

  • 记录自己在xss-labs的通关记录
    第十一关(referer)直接查看网页源代码,发现四个input被隐藏,不难看出,第四个名为t_ref的<input>标签是http头referer的参数(就是由啥地址转跳到这里的,http头的referer会记录有)通过构造payload,配合burpsuite抓包可以得到?keyword=&t_link"type='text'>//&t_history"type='text'>......
  • Apache JMeter 压测工具使用记录
    目录ApacheJMeter压测工具使用记录参考资料官方网站JMeter是什么?JMeter特性3使用jmeter3.1安装jmeter3.2添加一个http测试方案3.2.1调整测试方案名3.2.2添加线程组3.2.3添加HTTP采样器3.2.4添加结果监听器3.2.5添加汇总报告3.3执行测试方案ApacheJMeter压测......
  • 【四种语言一网打尽(C\C++\Python\Golang)】L1-005 考试座位号
    L1-005考试座位号每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着......
  • C语言学习记录(七)————控制语句
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言控制语句是什么?一、分支语句(选择结构)1if-else语句2switch语句二、循环语句1.goto2while3do-while4for三、辅助控制语句1break2continue3return总结前言一位学习C语言的小白,有......
  • C语言学习记录(六)————输入输出
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、输入输出1.数据输出2.格式输出函数3.字符输入函数4格式输入函数5输入函数垃圾清理方法1:用getchar清除方法2:用格式串中空格或“%*c”来吃掉6字符串输入输出函数6.1字符串输入函数get......
  • ATcoder ABC 351 补题记录(A~F)
    A按照顺序直接模拟即可。#pragmaGCCoptimize(3)#include<bits/stdc++.h>#defineintlonglong#definepbpush_back#defineememplace_back#defineF(i,x,y)for(inti=x;i<=y;i++)#defineG(i,x,y)for(inti=x;i>=y;i--)#defineW(G,i,x)for(auto&i:G[x......
  • 深度学习入门(鱼书)学习记录 - 第5章 误差反向传播法
    前言:上一章通过数值微分计算神经网络的权重参数的梯度,这种方法比较简单但比较耗时。所以现在介绍另外一种比较高效的方法-- 误差反向传播法目录计算图举例为什么用计算图求解计算图的优点链式法则链式求导反向传播加法节点的反向传播乘法节点的反向传播简单层的......
  • 【ROS使用记录】—— ros使用过程中的rosbag录制播放和ros话题信息相关的指令与操作记
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、rosbag的介绍二、rosbag的在线和离线录制三、rosbag的播放相关的指令四、其他rosbag和ros话题相关的指令总结前言rosbag是ROS(机器人操作系统)中用于记录和回放数据的工具。录制数据可......
  • CCF-GESP 等级考试 2023年9月认证C++四级真题解析
    一、单选题(每题2分,共30分)第1题⼈们所使⽤的⼿机上安装的App通常指的是()。A.⼀款操作系统B.⼀款应⽤软件C.⼀种通话设备D.以上都不对正确答案:B.⼀款应⽤软件解析:App是"Application"的缩写,中文意思是"应用",特指安装在智能手机上的第三方应用软件。这些软件通常......
  • 记录自己在xss-labs靶场的通关记录
    一:靶场下载及搭建xss-labs下载地址:xss-labs:xss跨站漏洞平台-GitCodephpstudy集成开发环境安装:[靶场环境篇]phpstudy集成环境安装教程(特别详细)_phpstudy集成环境-CSDN博客我们下载完之后,就可以进行xss-labs-master的搭建,我本人下载的phpstudy是2018年版的将xss-......