Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) C
Description
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
The first line of input contains the only positive integer n (1 ≤ n ≤ 2·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R — to the right, U — to the top and D — to the bottom. Have a look at the illustrations for better explanation.
The only line of input should contain the minimum possible length of the sequence.
4
RURD
2
6
RRULDD
2
26
RRRULURURUULULLLDLDDRDRDLD
7
3
RLL
2
4
LRLR
4
The illustrations to the first three tests are given below.
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.
题意:不明白
解法:每运动到一个点,如果之前有反向就+1,清空标记,然后记录当前方向,依次进行下去
#include<bits/stdc++.h> using namespace std; int n; string s; map<char,int>q; int main() { int flag=0; cin>>n; cin>>s; int num=1; for(int i=n-1; i>=0; i--) { flag=0; // q.clear(); if(s[i]=='R') { if(q['L']==1) { flag=1; // num++; // q['L']=0; // continue; } } if(s[i]=='L') { if(q['R']==1) { flag=1; // num++; // q['R']=0; // continue; } } if(s[i]=='D') { if(q['U']==1) { flag=1; // num++; // q['U']=0; // continue; } } if(s[i]=='U') { if(q['D']==1) { flag=1; // num++; // q['D']=0; // continue; } } if(flag==1) { num++; q.clear(); } q[s[i]]=1; } cout<<num<<endl; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1008
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1007
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1006
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1005
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1001 1002 1010